如何将以下数组数据分组为上述结构?

时间:2019-05-14 19:01:28

标签: javascript arrays performance ecmascript-6

目前,我有一个数组数据结构,如下面“实际”所述。希望以某种方式将其分组以获取所需的结构,如下文“预期”所述。

我尝试使用lodash groupBy,mapToValues和map将其排列在所需的结构中,但无法实现。

实际:

const candyDataSoldByEachDayInYear2000 = [
  {
    candyName: 'snickers',
    data: [{
      date: "2000-01-01", sold: 100
    }, {
      date: "2000-01-02", sold: 110
    }]
  }, {
    candyName: 'kitkat',
    data: [{
      date: "2000-01-01", sold: 80
    }, {
      date: "2000-01-02", sold: 70
    }]
  }, {
    candyName: 'twix',
    data: [{
      date: "2000-01-01", sold: 60
    }, {
      date: "2000-01-02", sold: 50
    }]
  }, {
    candyName: 'milkyway',
    data: [{
      date: "2000-01-01", sold: 60
    }, {
      date: "2000-01-02", sold: 70
    }]
  }, {
    candyName: 'skittles',
    data: [{
      date: "2000-01-01", sold: 120
    }, {
      date: "2000-01-02", sold: 110
    }]
  }];

预期:

const candyDataSoldByEachDayInYear2000 = [
  {
    date: "2000-01-01",
    candySold: {
      snickers: 100,
      kitkat: 80,
      twix: 60,
      milkyway: 60,
      skittles: 120
    }
  },
  {
    date: "2000-01-02",
    candySold: {
      snickers: 110,
      kitkat: 70,
      twix: 50,
      milkyway: 70,
      skittles: 110
    }
  }
];

2 个答案:

答案 0 :(得分:1)

您可以将Array.reduce()Array.forEach()一起使用,以创建带有日期键的对象,将糖果添加到相关日期,并使用Array.values()转换为数组:

const candyDataSoldByEachDayInYear2000 = [{"candyName":"snickers","data":[{"date":"2000-01-01","sold":100},{"date":"2000-01-02","sold":110}]},{"candyName":"kitkat","data":[{"date":"2000-01-01","sold":80},{"date":"2000-01-02","sold":70}]},{"candyName":"twix","data":[{"date":"2000-01-01","sold":60},{"date":"2000-01-02","sold":50}]},{"candyName":"milkyway","data":[{"date":"2000-01-01","sold":60},{"date":"2000-01-02","sold":70}]},{"candyName":"skittles","data":[{"date":"2000-01-01","sold":120},{"date":"2000-01-02","sold":110}]}];
  
const result = Object.values(candyDataSoldByEachDayInYear2000.reduce((r, { candyName, data }) => {
  data.forEach(({ date, sold }) => {
    if(!r[date]) r[date] = { date, candySold: {} };
    
    const cs = r[date].candySold;
    
    cs[candyName] = (cs[candyName] || 0) + sold;
  });

  return r;
}, {}));

console.log(result);

答案 1 :(得分:0)

这是不使用库的一种方法:

const collectSales = (candy) => Object .entries (candy .reduce (
  (a, {candyName, data}) => data .reduce (
    (a, {date, sold, o = a[date] || {}, c = o[candyName] || 0}) => 
      ({...a, [date]: {...o, [candyName]: c + sold}}),
    a
  ),
  {}
))
.map ( ([date, candySold]) => ({date, candySold}) )

const candyDataSoldByEachDayInYear2000 = [{candyName:"snickers",data:[{date:"2000-01-01",sold:100},{date:"2000-01-02",sold:110}]},{candyName:"kitkat",data:[{date:"2000-01-01",sold:80},{date:"2000-01-02",sold:70}]},{candyName:"twix",data:[{date:"2000-01-01",sold:60},{date:"2000-01-02",sold:50}]},{candyName:"milkyway",data:[{date:"2000-01-01",sold:60},{date:"2000-01-02",sold:70}]},{candyName:"skittles",data:[{date:"2000-01-01",sold:120},{date:"2000-01-02",sold:110}]}];

console .log (
  collectSales (candyDataSoldByEachDayInYear2000)
)

此版本使用reduce将数据转换为以下内容:

{
    "2000-01-01": {
        kitkat: 80,
        milkyway: 60,
        skittles: 120,
        snickers: 100,
        twix: 60
    },
    "2000-01-02": {
        kitkat: 70,
        milkyway: 70,
        skittles: 110,
        snickers: 110,
        twix: 50
    }
}

,然后将结果包装在Object.entries之后,再加上简单的map语句。

更新

以上是从此版本更改的:

const collectSales = (candy) => Object.entries ( candy .reduce (
  (a, {candyName, data}) => data .reduce (
    (a, {date, sold}) => 
      ({...a, [date]: {...(a[date] || {}), [candyName]: ((a[date] || {})[candyName] || 0) + sold}}),
    a
  ),
  {}
))
.map ( ([date, candySold]) => ({date, candySold}) )

const candyDataSoldByEachDayInYear2000 = [{candyName:"snickers",data:[{date:"2000-01-01",sold:100},{date:"2000-01-02",sold:110}]},{candyName:"kitkat",data:[{date:"2000-01-01",sold:80},{date:"2000-01-02",sold:70}]},{candyName:"twix",data:[{date:"2000-01-01",sold:60},{date:"2000-01-02",sold:50}]},{candyName:"milkyway",data:[{date:"2000-01-01",sold:60},{date:"2000-01-02",sold:70}]},{candyName:"skittles",data:[{date:"2000-01-01",sold:120},{date:"2000-01-02",sold:110}]}];

console .log (
  collectSales (candyDataSoldByEachDayInYear2000)
)

这是明确的清理工作,避免了某些丑陋的重复。