如何从json数组中提取值:
[{"id":1,"name":"Blue"}, {"id":2,"name":"Green"}, {"id":3,"name":"Red"}]
并仅按以下格式返回ID:
['1','2','3','4','5']
答案 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;