我有相聯繫的數組:訪問對象值的關聯數組值
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) –