The benefit of arrays is that you'll ensure order, but in your case,
there is no need to ensure order.
( Not sure if JSON objects (hashes) ensure order... do all browsers
keep order? )
On the topic of optimization, you don't need to evaluate the length of
the array every time so
for (var i=0; i < myNotes.length; i++){ ... }
can be rewritten as
for (var i=0, _len = myNotes.length; i < _len; i++){ ... }
it's a good habit.
jimmy
On Sep 17, 9:51 pm, Rey Bango <[email protected]> wrote:
> Ah good idea!
>
> On Sat, Sep 17, 2011 at 9:34 PM, Michal Kuklis <[email protected]>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/**[email protected]/<http://www.mail-archive.com/[email protected]/>
>
> >> To search via a non-Google archive, visit here:
> >>http://www.mail-archive.com/**[email protected]/<http://www.mail-archive.com/[email protected]/>
>
> >> To unsubscribe from this group, send email to
> >> jsmentors+unsubscribe@**googlegroups.com<jsmentors%2Bunsubscribe@googlegrou
> >> ps.com>
>
> > --
> > To view archived discussions from the original JSMentors Mailman list:
> > http://www.mail-archive.com/**[email protected]/<http://www.mail-archive.com/[email protected]/>
>
> > To search via a non-Google archive, visit here:
> >http://www.mail-archive.com/**[email protected]/<http://www.mail-archive.com/[email protected]/>
>
> > To unsubscribe from this group, send email to
> > jsmentors+unsubscribe@**googlegroups.com<jsmentors%2Bunsubscribe@googlegrou
> > ps.com>
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]