从对象数组返回属性的子集 [英] Returning subset of properties from an array of objects

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

问题描述

我有一个类似

var array = [{date:'01/01/2017',value1:200,value2:300,value3:400}]

我正在尝试获取对象属性的子集,例如var

I am trying to get a subset of the object properties like var

var newArray = [['01/01/2017',200],['01/01/2017',200],['01/01/2017',200]......]

我不想要这样的数组

[[date:'',value2:],[date:'',value2:],[date:'',value13:]]

但是直接是对象数组中的2D数组.

But just directly a 2 D array from array of objects.

目前,我正在对对象数组中的每个对象进行操作,并将所需的属性推入数组中,然后返回该数组.

Currently I am doing a for each on my array of objects and pushing the required properties in to an array an returning that array.

我在寻找地图功能可能是可以的,但这不适用于地图

I was looking for map function may be if that can work but this does not work with map

array.map(function(item){ 
return {
      item.date, item.value1
       }
});

请提示是否还有其他功能可以在不进行循环的情况下进行此操作?

Kindly suggest if there is any other function to do this without looping over?

谢谢

推荐答案

为此,您应该使用map,您快到了.这样可以解决您的问题:

You should use map for this, you were almost there. This will sort you out:

array.map(function(item){ return [item.date,item.value1]});

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

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