关联数组对象上的 JavaScript foreach 循环
2022-08-30 01:09:58
为什么我的 for-each 循环不迭代我的 JavaScript 关联数组对象?
// Defining an array
var array = [];
// Assigning values to corresponding keys
array["Main"] = "Main page";
array["Guide"] = "Guide page";
array["Articles"] = "Articles page";
array["Forum"] = "Forum board";
// Expected: loop over every item,
// yet it logs only "last" assigned value - "Forum"
for (var i = 0; i < array.length; i++) {
console.log(array[i]);
}
jQuery可能会有所帮助:https://api.jquery.com/jQuery.each/each()