javascript 如何使用 Ext.iterate(或 JS)迭代对象数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21674735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:27:26  来源:igfitidea点击:

How to iterate over an Array of objects with Ext.iterate (or JS)?

javascriptarraysextjs

提问by Stranded Kid

I know there is plenty of answers for this question on the net, but still I can't make it work.

我知道网络上有很多关于这个问题的答案,但我仍然无法解决这个问题。

After a callback on a JSON POST, I'm retrieving an Array of Object. On the console log of the browser, the array is like this :

在 JSON POST 回调之后,我正在检索一个对象数组。在浏览器的控制台日志中,数组是这样的:

 [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

where inside each object, there is stuff like :

在每个对象内部,都有类似的东西:

 id4blob: "1084"

so I assume the array could be like this (correct me if I'm wrong):

所以我假设数组可能是这样的(如果我错了,请纠正我):

 [{id4blob: "1084"},{id4blob: "2008"}, [...]]

The array is stored in a variable called stationsParse

该数组存储在一个名为stationsParse的变量中

I want to iterate over these values, I've found

我想迭代这些值,我发现

  Ext.iterate

So I did this :

所以我这样做了:

Ext.iterate(stationsParse, function(key, value) {
   console.log(key + value);
});

and then tried this (which seems yo be the same but in pure JS)

然后尝试了这个(这似乎是一样的,但在纯 JS 中)

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

But none is working, nothing is displayed in my console. Still, the function is well triggered, I can see it into Chrome console, but the for is ignored.

但是没有一个工作,我的控制台中没有显示任何内容。尽管如此,该功能还是被很好地触发了,我可以在 Chrome 控制台中看到它,但忽略了 for。

What Am I doing wrong ? Is this the correct way to iterate over an array of objects ?

我究竟做错了什么 ?这是迭代对象数组的正确方法吗?

回答by user1578653

Try something like this:

尝试这样的事情:

var stationsParse = [{id: 1, someproperty: 2}, {id: 2, someproperty: 101}];

Ext.each(stationsParse, function(ob){
  Ext.Object.each(ob, function(property, value){
    console.log(property, value);
  });
});

Here I am using Ext.each to iterate over each element in the stationsParse array. Each element in that array is an object. To iterate over each property in the object I use Ext.Object.each. Both 'each' functions take the variable you want to iterate over as its first parameter and a function as its second parameter. The function will be run on each element of the array/property of the object.

在这里,我使用 Ext.each 迭代stationsParse 数组中的每个元素。该数组中的每个元素都是一个对象。为了遍历对象中的每个属性,我使用 Ext.Object.each。两个“每个”函数都将要迭代的变量作为第一个参数,将函数作为第二个参数。该函数将在对象的数组/属性的每个元素上运行。

回答by Andy

If you want to use vanilla JS to loop over an array, a simple forloop will do the the trick. Access the value of each object's id4blobproperty using dot notation.

如果你想使用 vanilla JS 循环遍历一个数组,一个简单的for循环就可以解决问题。id4blob使用点表示法访问每个对象的属性值。

for (var i = 0, l = arr.length; i < l; i++) {
  console.log(arr[i].id4blob)
}

The for...inloop is generally reserved for looping over an object's properties.

所述for...in环通常被保留以遍历对象的属性。

Demo

演示

回答by Einar

I was able to make this work:

我能够完成这项工作:

for (var i in family) {
    console.log( family[i]["name"] );
}

in this tutorial on Codecademy, where familyis an array of objects.

Codecademy 的本教程中,其中family是对象数组。

The only difference I can see is the varin front of my iterator variable i.

我能看到的唯一区别是var在我的迭代器变量前面i