繁体   English   中英

将对象数组映射到按时间戳分组的新结构

[英]Map array of objects to new structure grouped by timestamp

给定以下对象数组:

const array = [
  {
    timestamp: '2016-01-01',
    itemID: 'AA',
    value: 20,
  },
  {
    timestamp: '2016-01-01',
    itemID: 'AB',
    value: 32,
  },
  {
    timestamp: '2016-01-02',
    itemID: 'ABC',
    value: 53
  },
  {
    timestamp: '2016-01-02',
    itemID: 'ABCD',
    value: 51
  }
]

我想返回以下结果:

const result = [
  {
    timestamp: '2016-01-01',
    AA: 20,
    AB: 32,
  },
  {
    timestamp: '2016-01-02',
    ABC: 53,
    ABCD: 51
  }
]

我设法做了以下事情:

 const array = [ { timestamp: '2016-01-01', itemID: 'AA', value: 20, }, { timestamp: '2016-01-01', itemID: 'AB', value: 32, }, { timestamp: '2016-01-02', itemID: 'ABC', value: 53 }, { timestamp: '2016-01-02', itemID: 'ABCD', value: 51 } ] var res = []; array.forEach(function(element) { var e = res.find(function(e) { return e.timestamp == element.timestamp; }); console.log(e) if(e) { } else { res.push({ [element.timestamp]: element.timestamp, [element.itemID]: element.value }); } }); console.log(res, ''); 

您可以创建一个对象 - 按时间戳分组并将其映射回数组,如:

let mapByTimestamp = array.reduce((res, item) => {
  res[item.timestamp] = res[item.timestamp] || {};

  Object.assign(res[item.timestamp], {
    [item.itemID]: item.value
  });

  return res;
}, {});

Object.keys(mapByTimestamp)
    .map(timestamp => Object.assign(mapByTimestamp[timestamp], { timestamp }));

您需要在不存在时插入并在更改时进行更新:

var output = [];

for (var index in array) {
    var found = false;
    for (var innerIndex in output) {
        if (output[innerIndex].timestamp === array[index].timestamp) {
            output[innerIndex][array[index].itemID] = array[index].value;
            found = true;
        }
    }
    if (found) {
        var newItem = {timestamp: array[index].timestamp};
        newItem[array[index].itemID] = array[index].value;
        output.push(newItem);
    }
}

您可以使用地图来收集值。

 var array = [{ timestamp: '2016-01-01', itemID: 'AA', value: 20 }, { timestamp: '2016-01-01', itemID: 'AB', value: 32 }, { timestamp: '2016-01-02', itemID: 'ABC', value: 53 }, { timestamp: '2016-01-02', itemID: 'ABCD', value: 51 }], grouped = array.reduce((map => (r, a) => { var o = { timestamp: a.timestamp }; if (!map.has(a.timestamp)) { map.set(a.timestamp, o); r.push(o); } map.get(a.timestamp)[a.itemID] = a.value; return r; })(new Map), []); console.log(grouped); 

array.forEach(function(element) {
  var e = res.find(function(data) {
  return data.timestamp == element.timestamp;
});

if(e) {
  e[element.itemID] =  element.value;
} else {
 res.push({
   timestamp: element.timestamp,
   [element.itemID]: element.value
 });
}   

});

var res = [];
array.forEach(function(element) {
  var e = res.find(function(e){
    return e.timestamp == element.timestamp;
  });
  if(e){
    e[element.itemID] = element.value;
  }
  else{
    e = {};
    e.timestamp = element.timestamp;
    e[element.itemID] = element.value;
    res.push(e);
  }
});

 const array = [ { timestamp: '2016-01-01', itemID: 'AA', value: 20, }, { timestamp: '2016-01-01', itemID: 'AB', value: 32, }, { timestamp: '2016-01-02', itemID: 'ABC', value: 53 }, { timestamp: '2016-01-02', itemID: 'ABCD', value: 51 } ] var res = []; array.forEach(function(element) { var e = res.find(function(e) { return e.timestamp == element.timestamp; }); if(e === undefined){ res.push({ 'timestamp':element.timestamp, [element.itemID]:element.value }); }else{ e[element.itemID] = element.value; } }); console.log(res, ''); 

在那里,我修复了你的代码。 :)

你可以这样做;

 var array = [ { timestamp: '2016-01-01', itemID: 'AA', value: 20, }, { timestamp: '2016-01-01', itemID: 'AB', value: 32, }, { timestamp: '2016-01-02', itemID: 'ABC', value: 53 }, { timestamp: '2016-01-02', itemID: 'ABCD', value: 51 } ], interim = array.reduce((p,c) => Object.assign(p, {[c.timestamp]: p[c.timestamp] ? Object.assign(p[c.timestamp], {[c.itemID]: c.value}) : {[c.itemID]: c.value, timestamp: c.timestamp}}),{}); result = Object.keys(interim) .map(k => interim[k]); console.log(result); 

暂无
暂无

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

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