Ah good idea!

On Sat, Sep 17, 2011 at 9:34 PM, Michal Kuklis <michal.kuk...@gmail.com>wrote:

> Hey Rey, one way to improve it would be to simplify your JSON structure a
> bit for example:
> var notes = {"A": {"maj7": [0, 3, 2, 0, 1, 2], "min": [0, 3, 2, 0, 1, 2]},
> "B": {"min": [0, 3, 2, 0, 1, 2]}};
>
> this way you could just do:
>
>
> function getChord( myNotes, theNote, theChord ) {
>    return myNotes[theNote][theChord];
> }
>
> Michal
>
>
>
> On 9/18/11 12:19 AM, Rey Bango wrote:
>
>> Hey guys,
>>
>> The code below loops over some JSON data. It' simple in that it looks for
>> a specific note and then a specific chord within the note. It then returns
>> the strings.
>>
>> I'm using for loops to handle it but wanted to ask if there was a better,
>> faster or more efficient way to do it.
>>
>> Thoughts?
>>
>> Rey...
>>
>>
>> function getChord( myNotes, theNote, theChord ) {
>> var theChords = '';
>>
>>    for (var i=0; i < myNotes.length; i++) {
>>        if (myNotes[i].note == theNote) {
>>                        theChords = myNotes[i].chords;
>>            for (var i=0; i < theChords.length; i++) {
>>                if (theChords[i].chord == theChord) {
>>                    return theChords[i].strings;
>>                };
>>
>>            };
>>
>>        };
>>
>>    };
>>
>> }
>>
>>
>> var notes=          [{
>>    "note": "A",
>>    "chords": [
>>        {
>>            "chord": "maj7",
>>            "strings": {
>>                "1": 0,
>>                "2": 3,
>>                "3": 2,
>>                "4": 0,
>>                "5": 1,
>>                "6": 2
>>            }
>>        },
>>        {
>>            "chord": "min",
>>            "strings": {
>>                "1": 0,
>>                "2": 3,
>>                "3": 2,
>>                "4": 0,
>>                "5": 1,
>>                "6": 2
>>            }
>>        }
>>    ]
>> },{
>>    "note": "B",
>>    "chords": [
>>        {
>>            "chord": "maj7",
>>            "strings": {
>>                "1": 0,
>>                "2": 3,
>>                "3": 2,
>>                "4": 0,
>>                "5": 1,
>>                "6": 2
>>            }
>>        },
>>        {
>>            "chord": "min",
>>            "strings": {
>>                "1": 0,
>>                "2": 3,
>>                "3": 2,
>>                "4": 0,
>>                "5": 1,
>>                "6": 2
>>            }
>>        }
>>    ]
>> }];
>>
>>
>> chordStrings = getChord( notes, "A", "maj7" );
>>
>> document.write( chordStrings );
>>
>>
>>
>>
>> --
>> To view archived discussions from the original JSMentors Mailman list:
>> http://www.mail-archive.com/**jsment...@jsmentors.com/<http://www.mail-archive.com/jsmentors@jsmentors.com/>
>>
>> To search via a non-Google archive, visit here:
>> http://www.mail-archive.com/**jsmentors@googlegroups.com/<http://www.mail-archive.com/jsmentors@googlegroups.com/>
>>
>> To unsubscribe from this group, send email to
>> jsmentors+unsubscribe@**googlegroups.com<jsmentors%2bunsubscr...@googlegroups.com>
>>
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/**jsment...@jsmentors.com/<http://www.mail-archive.com/jsmentors@jsmentors.com/>
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/**jsmentors@googlegroups.com/<http://www.mail-archive.com/jsmentors@googlegroups.com/>
>
> To unsubscribe from this group, send email to
> jsmentors+unsubscribe@**googlegroups.com<jsmentors%2bunsubscr...@googlegroups.com>
>

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to