21

我有一个数组,其中包含带有键的对象,值我们如何迭代每个对象 forcasteid

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

9 回答 9

38

使用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>

于 2013-04-26T10:54:34.470 回答
15

示例代码:

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

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

于 2013-04-26T10:46:19.647 回答
9

在纯 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.

Greetings.

于 2013-04-26T19:11:01.473 回答
4
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;
}
于 2013-04-26T10:45:32.883 回答
4

Arrow functions are modern these days

Using jquery $.each with arrow function

 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

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

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

  array.map((item, index) => {
      console.log('caste: ' + item.caste + ' | id: ' +item.id);
  });
于 2018-09-05T17:43:15.960 回答
2

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

 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);
于 2017-03-30T06:10:43.447 回答
1

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

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

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

于 2013-04-26T10:52:05.210 回答
0

使用jQuery.each

$.each([your array of objects], function(index, value) {
  // Do what you need, for example...
  alert(index + ': ' + value);
});
于 2013-04-26T10:46:33.280 回答
0

要遍历一个在 jQuery 中填充的数组,请使用$.each,要遍历一个 Object 的属性,请使用 for..in

于 2013-04-26T10:46:42.043 回答