值我有對象的一個這樣的數組:使用的console.log以顯示對象
Object {index: "4", value: -1}
Object {index: "0", value: 0}
Object {index: "6", value: 0}
Object {index: "8", value: 0}
Object {index: "1", value: 1}
Object {index: "3", value: 1}
Object {index: "5", value: 1}
Object {index: "7", value: 1}
非常第一個爲0的指標,我可以像console.log[array[0]
訪問它,這將現在給我Object {index: "4", value: -1}
如果不使用對象數組的索引,我想通過對象內部的value
或index
來訪問它。我嘗試了幾個不同的命令。
console.log(array["index: " + 8]
console.log(array["index:" == 8]
都未做任何東西,除了拋出未定義或錯誤的以上兩個例子,我想他們打印出來Object {index: "8", value: 0}
我希望這是有道理的
'console.log [array [0]'是各種破碎的語法。 – meagar
恐怕JavaScript不能以這種方式工作。你需要搜索和/或循環數組,以便做到這一點。 – Boaz
你需要編寫一個小函數來循環數組以找到一個元素,或者使用像jQuery這樣的外部庫。 jQuery的函數是'.inArray'。 SO – foibs