循环遍历一组对象

时间:2011-11-21 05:38:42

标签: javascript arrays object

我有一个像这样的对象数组 -

    [{"name":"admission[year]","value":"2011-12"},{"name":"admission[class]","value":"Nursery"}]

我如何访问名称价值对,如入学[年] - 2011-12,以及录取[class] - javascript的托儿所。

3 个答案:

答案 0 :(得分:2)

var x = [{"name":"admission[year]","value":"2011-12"},{"name":"admission[class]","value":"Nursery"}]
var i, len = x.length;
for(i = 0; i < len; i++)
    console.log(x[i].name + ': ' + x[i].value);

输出:

admission[year]: 2011-12
admission[class]: Nursery

IE。 x[0].name === "admission[year]"x[1].value === "Nursery"

答案 1 :(得分:0)

/*

using jquery you can do something like this 

*/

$.each( ['a','b','c'], function(key, value){
   alert( "Index #" + key + ": " + value );
 });

答案 2 :(得分:0)

var arr = [{ "name": "admission[year]", "value": "2011-12" }, { "name": "admission[class]", "value": "Nursery"}];
for (element in arr) {
    var combinedValue = arr[element].name + ' ' + arr[element].value;
    alert(combinedValue);
}