从多维数组中获取值

时间:2013-10-10 06:28:40

标签: javascript

我有一个数组,我只想从中得到名字。

var peoples = [
  { "name": "dod", "class": "a", "age": 12 },
  { "name": "john", "class": "b", "age": 14  },
  { "name": "henry", "class": "c", "age": 23 }
];

如何使用逗号分隔从每个对象获取名称?

3 个答案:

答案 0 :(得分:1)

在jquery中,

var peoples = [
  { "name": "dod", "class": "a", "age": 12 },
  { "name": "john", "class": "b", "age": 14  },
  { "name": "henry", "class": "c", "age": 23 }
];

var names = new Array();
$.each(peoples,function(key,value){
    names[key] = value.name;
});
namelist = names.join(",");
console.log(namelist);

http://jsfiddle.net/9344Q/

答案 1 :(得分:0)

这肯定会在普通的Javascript中实现:

var peoples = [
  { "name": "dod", "class": "a", "age": 12 },
  { "name": "john", "class": "b", "age": 14  },
  { "name": "henry", "class": "c", "age": 23 }
];

var arr = [];
peoples.forEach(function(name) {
    arr.push(name['name']);
});

console.log(arr.join(','));

答案 2 :(得分:0)

var peoples = [
  { "name": "dod", "class": "a", "age": 12 },
  { "name": "john", "class": "b", "age": 14  },
  { "name": "henry", "class": "c", "age": 23 }
];
alert(peoples.map( function(v){ return v.name; }).join());