0

我有一个数组,我只想从中获取名称。

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

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

4

3 回答 3

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/

于 2013-10-10T06:34:10.440 回答
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(','));
于 2013-10-10T06:34:44.737 回答
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());
于 2013-10-10T06:40:17.653 回答