为什么我的for- 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 each()可能有帮助:https://api.jquery.com/jQuery.each/


当前回答

下面是一个使用关联数组作为泛型对象类型的简单方法:

Object.prototype.forEach =函数(cb){ if(this instanceof Array)返回this. foreach (cb); Let self = this; Object.getOwnPropertyNames(这).forEach ( (k) = > {cb。调用(self, self[k], k);} ); }; 对象({1,b: 2, c: 3})。forEach((值,键)= > { Console.log('键/值对:${键}/${值}'); });

其他回答

这是一个非常简单的方法。好处是你也可以得到钥匙:

for (var key in array) {
    var value = array[key];
    console.log(key, value);
}

For ES6:

array.forEach(value => {
  console.log(value)
})

对于ES6(如果你想要值,索引和数组本身):

array.forEach((value, index, self) => {
  console.log(value, index, self)
})

length属性只跟踪带有数字索引(键)的属性。你用字符串作为键。

你可以这样做:

var arr_jq_TabContents = {}; // no need for an array

arr_jq_TabContents["Main"] = jq_TabContents_Main;
arr_jq_TabContents["Guide"] = jq_TabContents_Guide;
arr_jq_TabContents["Articles"] = jq_TabContents_Articles;
arr_jq_TabContents["Forum"] = jq_TabContents_Forum;

for (var key in arr_jq_TabContents) {
    console.log(arr_jq_TabContents[key]);
}

为了安全起见,在这样的循环中,确保没有任何属性是继承的意外结果是一个好主意:

for (var key in arr_jq_TabContents) {
  if (arr_jq_TabContents.hasOwnProperty(key))
    console.log(arr_jq_TabContents[key]);
}

edit -现在注意Object.keys()函数在现代浏览器和Node等中可用可能是个好主意。该函数返回对象的“自己的”键,作为数组:

Object.keys(arr_jq_TabContents).forEach(function(key, index) {
  console.log(this[key]);
}, arr_jq_TabContents);

The callback function passed to .forEach() is called with each key and the key's index in the array returned by Object.keys(). It's also passed the array through which the function is iterating, but that array is not really useful to us; we need the original object. That can be accessed directly by name, but (in my opinion) it's a little nicer to pass it explicitly, which is done by passing a second argument to .forEach() — the original object — which will be bound as this inside the callback. (Just saw that this was noted in a comment below.)

似乎几乎每一个答案都不是一开始问的。

foreach-loop似乎不起作用。简单的for循环也不能很好地工作,因为length属性在关联数组的情况下将为零(这是一种退步)。而for-in则用于关联数组

//定义数组 Var数组= []; //为相应的键赋值 array["Main"] = "主页"; array["Guide"] = "指南页"; array["Articles"] = "Articles page"; array["论坛"]= "论坛板"; //期望:遍历每一项, //但它只记录“最后”分配的值-“论坛” 对于(var index in array) { console.log(索引数组(指数)); }

你可以这样做:

var array = [];

// Assigning values to corresponding keys
array[0] = "Main page";
array[1] = "Guide page";
array[2] = "Articles page";
array[3] = "Forum board";


array.forEach(value => {
    console.log(value)
})

 

如果Node.js或浏览器支持Object.entries(),它可以用作Object.keys()的替代方案(Pointy的答案)。

Const h = { 答:1, b: 2 }; Object.entries (h)。forEach(([key, value]) => console.log(value)); // log 1, 2

在这个例子中,forEach使用数组的解构赋值。