Jquery从json数组解析值并返回逗号分隔

时间:2016-03-16 03:05:43

标签: jquery arrays json

如何从json数组中提取值:

[{"id":1,"name":"Blue"}, {"id":2,"name":"Green"}, {"id":3,"name":"Red"}]

并仅按以下格式返回ID:

['1','2','3','4','5']

1 个答案:

答案 0 :(得分:0)

试试这个,你可以在数组上使用map()函数



var x = [{
  "id": 1,
  "name": "Blue"
}, {
  "id": 2,
  "name": "Green"
}, {
  "id": 3,
  "name": "Red"
}];
var result = x.map(function(v) {
  return v.id + ''
})
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>')
&#13;
&#13;
&#13;