0

I have an associate array of :

  instrumentLookup: {
    hh: {label: 'Hi Hat', type: 'hh'},
    kd: {label: 'Kick Drum', type: 'kd'},
    o1: {label: 'other1', type: 'o1'},
    o2: {label: 'other2', type: 'o2'}
  }

I think that this structure is OK, but there may be a better way.

I am trying to create an instrument from this list by this function, where the param addedInstrument comes in as the same string as the label, so hh, kd, o1, ....:

addInstrument: function(addedIntrument) {
  console.warn(addedIntrument);
  var newLabel = this.defaults.instrumentLookup.addedIntrument.label;
  var newType = addedIntrument;
  console.warn(newLabel + ' ' + newType)
  // push it to the list
  // this.unusedInstruments.push({label:newLabel, type:newType});
}

There are a few questions in this, feel free to answer any or all or suggest an alternative:

  • How do you access Object properties when the Object is the value of an associative array?
  • Should I change it to an array[] of nested objects{type: {other attrs}} from an Associate array?
chris Frisina
  • 19,086
  • 22
  • 87
  • 167
  • 1
    Remember that JavaScript objects are true [Associative Arrays](http://en.wikipedia.org/wiki/Associative_array) (e.g. Maps that take strings as keys) and properties are not "indexed" as in PHP's definition of "associative array". – Paul Jul 04 '13 at 16:21
  • possible duplicate of [Dynamic object property name](http://stackoverflow.com/questions/4244896/dynamic-object-property-name) – Felix Kling Jul 04 '13 at 16:24

2 Answers2

3

You need the bracket notation to dynamically access property names.

instrumentLookup[ addedIntrument ].label
jAndy
  • 231,737
  • 57
  • 305
  • 359
2

How do you access Object properties when the Object is the value of an associative array?

It's pretty easy. You can do: console.log(instrumentLookup.hh.label); //Hi Hat

or console.log(instrumentLookup.hh['label']); //Hi Hat

Should I change it to an array[] of nested objects{type: {other attrs}} from an Associate array?

You should use an array, if you need array behavior.

From comments:

then why doesn't instrumentLookup.addedIntrument.label work?

addedInstrument is not a member of instrumentLookup, thus you can't use the . to access it (it will be undefined). Instead you need to do: instrumentLookup[addedInstrument]

I think that this structure is OK, but there may be a better way.

You are already storing each instrument by the type parameter, so why duplicate it inside the object? Unless you have more than just Type and Label, you could simplify this even further by:

var instrumentLookup = { hh: 'High Hat', 
                         kd: 'Kick Drum', 
                         o1: 'Other1', 
                         o2: 'Other2'}

and rewrite your addInstrument method:

addInstrument: function(addedInstrument) {
  console.warn(addedIntrument);
   var inst = this.defaults.instrumentLookcup[addedInstrument];
   if(inst) {
     this.unusedInstruments.push({label:inst, type:addedInstrument});
   }
}
Alan
  • 45,915
  • 17
  • 113
  • 134