当前位置:   jquery > 正文

将对象数组转换为属性数组

javascript,arrays,json,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,二维码生成,unix时间戳,在线开发工具,前端开发工具,开发人员工具,站长工具

是否有一种简单的方法,使用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);

上面的代码片段已更改为使其正常工作.



1> Praveen Kuma..:

使用.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);
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/jquery/detail/13567
推荐阅读
相关标签
  

闽ICP备14008679号