简体   繁体   English

从对象数组返回属性的子集

[英]Returning subset of properties from an array of objects

I have an array of objects like 我有一系列对象

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

I am trying to get a subset of the object properties like var 我试图获取像var这样的对象属性的子集

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

I do not want an array like this 我不想要这样的数组

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

But just directly a 2 D array from array of objects. 但是直接是对象数组中的2D数组。

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? 请建议是否还有其他功能可以不进行循环?

Thanks 谢谢

You should use map for this, you were almost there. 您应该为此使用map ,您快到了。 This will sort you out: 这样可以解决您的问题:

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

You need to put the values in an array & map method will do rest of the work 您需要将值放入数组中,并且map方法将完成其余工作

 var array = [{ date: '01/01/2017', value1: 200, value2: 300, value3: 400 }, { date: '01/01/3017', value1: 500, value2: 300, value3: 400 }]; var m = array.map(function(item) { return [item.date, item.value1] }) console.log(m) 
 [['01/01/2017',200],['01/01/2017',200]] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM