在Javascript中组合对象数组的属性

时间:2017-09-29 14:32:50

标签: javascript

我有这个数据集:

var dataset = [
{time: "t1", val1: 0, val2: 0, val3: 1},
{time: "t2", val1: 0, val2: 0, val3: 4},
{time: "t3", val1: 1, val2: 0, val3: 0},
{time: "t4", val1: 3, val2: 0, val3: 0},
{time: "t2", val1: 0, val2: 3, val3: 0},
{time: "t4", val1: 0, val2: 2, val3: 0},
{time: "t5", val1: 2, val2: 0, val3: 0},

....
];

我想将数据集缩减为唯一时间条目,但也要将每个时间步长的值组合在一起。结果必须如下所示:

var dataset = [
{time: "t1", val1: 0, val2: 0, val3: 1},
{time: "t2", val1: 0, val2: 3, val3: 4},
{time: "t3", val1: 1, val2: 0, val3: 0},
{time: "t4", val1: 3, val2: 2, val3: 0},
{time: "t5", val1: 2, val2: 0, val3: 0},
....
];

我尝试使用基于其他SO帖子的reduce()方法:

dataset.reduce(function(r,o){
var key = o.Date;
var newObj = o;
if (r[key] || (r[key]=[])) r[key].push(o);
  return r;
}, []);

console.log(dataset);

它将数据集减少到5个条目,但结构并不是我想要的结果,我不知道如何组合对象值。

感谢任何帮助。

3 个答案:

答案 0 :(得分:4)

Array.reduce()变体:



var dataset = [
    {time: "t1", val1: 0, val2: 0, val3: 1}, {time: "t2", val1: 0, val2: 0, val3: 4}, 
    {time: "t3", val1: 1, val2: 0, val3: 0}, {time: "t4", val1: 3, val2: 0, val3: 0}, 
    {time: "t5", val1: 2, val2: 0, val3: 0}, {time: "t2", val1: 0, val2: 3, val3: 0}, 
    {time: "t4", val1: 0, val2: 2, val3: 0}
],
result = [];

dataset.reduce(function(r, o){
   var k = o.time;   // crucial key (used for grouping entries)
   if (r[k]) {
       r[k].val1 +=o.val1;
       r[k].val2 +=o.val2;
       r[k].val3 +=o.val3;
   } else {
       result.push(o);
       r[k] = o;
   }
   return r;
}, {});

console.log(result);




答案 1 :(得分:1)

您可以将每个迭代步骤中的time值保存到临时keys属性中:

var dataset = [
    { time: "t1", val1: 0, val2: 0, val3: 1 },
    { time: "t2", val1: 0, val2: 0, val3: 4 },
    { time: "t3", val1: 1, val2: 0, val3: 0 },
    { time: "t4", val1: 3, val2: 0, val3: 0 },
    { time: "t5", val1: 2, val2: 0, val3: 0 },
    { time: "t2", val1: 0, val2: 3, val3: 0 },
    { time: "t4", val1: 0, val2: 2, val3: 0 },
];

var newData = dataset.reduce(function (r, o) {
    if (r.keys[o.time] !== true) {
        r.data.push(o);
    }
    r.keys[o.time] = true;
    return r;
}, { data: [], keys: {} }).data;

console.log(newData);
//[ { time: 't1', val1: 0, val2: 0, val3: 1 },
//  { time: 't2', val1: 0, val2: 0, val3: 4 },
//  { time: 't3', val1: 1, val2: 0, val3: 0 },
//  { time: 't4', val1: 3, val2: 0, val3: 0 },
//  { time: 't5', val1: 2, val2: 0, val3: 0 } ]

答案 2 :(得分:0)

您可以像示例

一样使用Array methods

var dataset = [
{time: "t1", val1: 0, val2: 0, val3: 1},
{time: "t2", val1: 0, val2: 0, val3: 4},
{time: "t3", val1: 1, val2: 0, val3: 0},
{time: "t4", val1: 3, val2: 0, val3: 0},
{time: "t5", val1: 2, val2: 0, val3: 0},
{time: "t2", val1: 0, val2: 3, val3: 0},
{time: "t4", val1: 0, val2: 2, val3: 0}
];

var datasetReduced=[];

dataset.forEach(function(dataItem){
  var reducedItem = datasetReduced.find(function(element){
    return element.time === dataItem.time;
  });
  if(reducedItem){
    
    reducedItem.val1= reducedItem.val1 + dataItem.val1;
    reducedItem.val2= reducedItem.val2 + dataItem.val2;
    reducedItem.val3= reducedItem.val3 + dataItem.val3;
  }else{
    datasetReduced.push(dataItem);
  }
});
console.log(datasetReduced);