var associativeArray = [];
associativeArray['key1'] = 'value1';
associativeArray['key2'] = 'value2';
associativeArray['key3'] = 'value3';
associativeArray['key4'] = 'value4';
associativeArray['key5'] = 'value5';
var key = null;
for(key in associativeArray)
{
console.log("associativeArray[" + key + "]: " + associativeArray[key]);
}
key = 'key3';
var obj = associativeArray[key];
// gives index = -1 in both cases why?
var index = associativeArray.indexOf(obj);
// var index = associativeArray.indexOf(key);
console.log("obj: " + obj + ", index: " + index);
上面的程序打印索引:-1,爲什麼?有沒有更好的方式來獲取關聯數組中的對象的索引而不使用循環?javascript:如何獲取關聯數組中的對象的索引?
如果我想從這個數組中刪除'key3'會怎麼樣?拼接函數將第一個參數作爲必須爲整數的索引。
JavaScript中沒有關聯數組。 – Sarfraz 2012-02-14 07:24:57
[在javascript對象中,獲取值的屬性的最佳方式是什麼?](http://stackoverflow.com/questions/9052888/in-a-javascript-object-whats-best-way-to -get-the-attribute-of-value) – user123444555621 2012-02-14 07:27:33
http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/ – 2012-02-14 07:35:10