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?有没有更好的方法来获取关联数组中对象的索引,而不使用循环?
如果我想从这个数组中删除“key3”,怎么办?splice函数的第一个参数必须是一个整数索引。