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);
Chương trình trên chỉ số in: -1, tại sao? Có cách nào tốt hơn để có được chỉ số của một đối tượng trong một mảng kết hợp mà không sử dụng vòng?javascript: cách lấy chỉ mục của đối tượng trong mảng liên kết?
Nếu tôi muốn xóa 'key3' khỏi mảng này thì sao? hàm ghép nối lấy tham số đầu tiên làm chỉ mục phải là một số nguyên.
Không có mảng liên kết trong javascript. – Sarfraz
có thể trùng lặp của [trong một đối tượng javascript, cách tốt nhất để có được thuộc tính của một giá trị là gì?] (Http://stackoverflow.com/questions/9052888/in-a-javascript-object-whats-best-way-to -get-the-attribute-of-a-value) – user123444555621
http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/ –