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