是否有一种简单的方法,使用filter
或者parse
其他东西来转换数组,如下所示:
var someJsonArray = [ {id: 0, name: "name", property: "value", otherproperties: "othervalues"}, {id: 1, name: "name1", property: "value1", otherproperties: "othervalues1"}, {id: 2, name: "name2", property: "value2", otherproperties: "othervalues2"} ];
进入一个简单的数组,填充前一个数组中包含的对象的一个属性,如下所示:
[0, 1, 2]
Praveen Kuma.. 43
使用.map()
功能:
finalArray = someJsonArray.map(function (obj) { return obj.id; });
片段
var someJsonArray = [
{id: 0, name: "name", property: "value", therproperties: "othervalues"},
{id: 1, name: "name1", property: "value1", otherproperties: "othervalues1"},
{id: 2, name: "name2", property: "value2", otherproperties: "othervalues2"}
];
var finalArray = someJsonArray.map(function (obj) {
return obj.id;
});
console.log(finalArray);
上面的代码片段已更改为使其正常工作.
使用.map()
功能:
finalArray = someJsonArray.map(function (obj) { return obj.id; });
片段
var someJsonArray = [
{id: 0, name: "name", property: "value", therproperties: "othervalues"},
{id: 1, name: "name1", property: "value1", otherproperties: "othervalues1"},
{id: 2, name: "name2", property: "value2", otherproperties: "othervalues2"}
];
var finalArray = someJsonArray.map(function (obj) {
return obj.id;
});
console.log(finalArray);