将对象数组转换为属性数组 [英] Convert array of objects into array of properties

查看:37
本文介绍了将对象数组转换为属性数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法,使用 filterparse 或其他东西来转换数组,如下所示:

Is there a simple way, using filter or parse or something else to convert an array like the following :

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"}
];

放入一个简单的数组,填充上一个数组中包含的对象的一个​​属性,如下所示:

into a simple array filled with one attribute of the objects contained in the previous array like this :

[0, 1, 2]

推荐答案

使用 .map() 函数:

Use .map() function:

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);

对以上代码段进行了更改以使其正常工作.

The above snippet is changed to make it work.

这篇关于将对象数组转换为属性数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆