我有相联系的数组:访问对象值的关联数组值
instrumentLookup: {
hh: {label: 'Hi Hat', type: 'hh'},
kd: {label: 'Kick Drum', type: 'kd'},
o1: {label: 'other1', type: 'o1'},
o2: {label: 'other2', type: 'o2'}
}
我认为这个结构确定,但有可能是一个更好的办法。
我想这个功能,在帕拉姆addedInstrument
就会出现,相同的字符串作为标签,所以hh
,,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});
}
有这几个问题,随时回答任何或全部或建议的替代:
- 如何访问对象属性当对象是关联数组的值?
- 我应该将其更改为来自Associate数组的嵌套对象{type:{other attrs}}的数组[]吗?
记住JavaScript对象是真正的[关联数组](http://en.wikipedia.org/wiki/Associative_array)和属性(即采取字符串作为键如地图)不是“索引”,如PHP的“关联数组”的定义。 – Paul
[动态对象属性名称](http://stackoverflow.com/questions/4244896/dynamic-object-property-name) –