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?
2
  • 1
    Remember that JavaScript objects are true Associative Arrays (e.g. Maps that take strings as keys) and properties are not "indexed" as in PHP's definition of "associative array". Commented Jul 4, 2013 at 16:21
  • possible duplicate of Dynamic object property name Commented Jul 4, 2013 at 16:24

2 Answers 2

3

You need the bracket notation to dynamically access property names.

instrumentLookup[ addedIntrument ].label
Sign up to request clarification or add additional context in comments.

Comments

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});
   }
}

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.