jQuery 在 javascript 中迭代对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16234445/
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-08-26 16:45:51  来源:igfitidea点击:

Iterating Array of Objects in javascript

javascriptjquery

提问by sasi

I am having an array that consists the objects with a key, value how can we iterate each object for casteand id.

我有一个数组,其中包含带有键、值的对象,我们如何为caste和迭代每个对象id

[
    Object {
        caste = "Banda",
        id = 4
    },
    Object {
        caste = "Bestha", 
        id = 6
    }
]

回答by RRikesh

Using jQuery.each():

使用jQuery.each()

var array = [
   {caste: "Banda", id: 4},
   {caste: "Bestha", id: 6}
];
    
$.each(array, function( key, value ) {
  console.log('caste: ' + value.caste + ' | id: ' +value.id);
}

);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

回答by Salvatorelab

Example code:

示例代码:

var list = [
    { caste:"Banda",id:4},
    { caste:"Bestha",id:6},
];
    
for (var i=0; i<list.length; i++) {
    console.log(list[i].caste);
}

It's just an array, so, iterate over it as always.

它只是一个数组,所以像往常一样迭代它。

回答by jortsc

In plain JavaScript you can do this:

在纯 JavaScript 中,您可以这样做:

var array = [{caste: "Banda", id: 4}, {caste: "Bestha", id:6}];

array.forEach(function(element, index) {
    console.log(element.id+" "+element.caste);
});

The callback function is called with a third parameter, the array being traversed. For learn more!

使用第三个参数调用回调函数,遍历数组。为了了解更多

So, you don't need to load jQuery library.

因此,您不需要加载 jQuery 库。

Greetings.

你好。

回答by Hemadri Dasari

Arrow functions are modern these days

现在箭头函数很现代

Using jquery $.each with arrow function

使用带有箭头功能的 jquery $.each

 var array = [
    {caste: "Banda", id: 4},
    {caste: "Bestha", id: 6}
 ];

 $.each(array, ( key, value ) => {
     console.log('caste: ' + value.caste + ' | id: ' +value.id);
 });

Using forEach with arrow function

使用带有箭头函数的 forEach

  array.forEach((item, index) => {
      console.log('caste: ' + item.caste + ' | id: ' +item.id);
  });

Using map with arrow function. Here map returns a new array

使用带有箭头功能的地图。这里 map 返回一个新数组

  array.map((item, index) => {
      console.log('caste: ' + item.caste + ' | id: ' +item.id);
  });

回答by package

var array = [{caste: "Banda", id: 4}, {caste: "Bestha", id:6}];
var length = array.length;
for (var i = 0; i < length; i++) {
   var obj = array[i];
   var id = obj.id;
   var caste = obj.caste;
}

回答by Mukkul Jayhne

The forEach loop accepts an iterator function and, optionally, a value to use as "this" when calling that iterator function.

forEach 循环接受一个迭代器函数和一个可选的值,在调用该迭代器函数时用作“this”。

 var donuts = [
    { type: "Jelly", cost: 1.22 },
    { type: "Chocolate", cost: 2.45 },
    { type: "Cider", cost: 1.59 },
    { type: "Boston Cream", cost: 5.99 }
];

donuts.forEach(function(theDonut, index) {
    console.log(theDonut.type + " donuts cost $"+ theDonut.cost+ " each");
});

Subsequently it can also be broken down to this

随后也可以分解成这样

var donuts = [
  { type: "Jelly", cost: 1.22 },
  { type: "Chocolate", cost: 2.45 },
  { type: "Cider", cost: 1.59 },
  { type: "Boston Cream", cost: 5.99 }
];


function ShowResults(donuts) {  
    console.log(donuts.type + " donuts cost $"+ donuts.cost+ " each");  
}

donuts.forEach(ShowResults);

回答by Joel Harkes

you can use jquery to iterate through all the objects jQuery wants you to fill a callback function, which jquery will call back. The first input parameter will be given the key and the second input parameter the value:

您可以使用 jquery 遍历 jQuery 希望您填充回调函数的所有对象,该回调函数将被 jquery 回调。第一个输入参数将被赋予键,第二个输入参数将被赋予值:

$.each(dataList, function(index, object) {
   $.each(object,function(attribute, value){
      alert(attribute+': '+value);
   });
});

documentation: http://api.jquery.com/jQuery.each/

文档:http: //api.jquery.com/jQuery.each/

回答by Alepac

Use jQuery.each:

使用jQuery.each

$.each([your array of objects], function(index, value) {
  // Do what you need, for example...
  alert(index + ': ' + value);
});

回答by LionC

To iterate over an array filled with stuff in jQuery use $.each, to iterate over an Object for its properties use for..in

要在 jQuery 中使用$.each遍历填充有内容的数组,请使用 for..in 遍历 Object 以获取其属性