【问题标题】:Transform array of Objects for React Nivo Bar Chart为 React Nivo 条形图转换对象数组
【发布时间】:2021-10-31 06:40:30
【问题描述】:

我有以下数据:

const rawData=[
  {
    Order_Date: "2020-08-11",
    Region: "South",
    Product_Name: "Bookcase",
    Sales: 261.96
  },
  {
    Order_Date: "2020-08-13",
    Region: "South",
    Product_Name: "Stacking Chairs",
    Sales: 731.94
  },
 {
    Order_Date: "2020-08-11",
    Region: "Central",
    Product_Name: "Table",
    Sales: 200
  },
  {
    Order_Date: "2020-08-11",
    Region: "East",
    Product_Name: "Chairs",
    Sales: 350
  },
  {
    Order_Date: "2020-10-06",
    Region: "South",
    Product_Name: "Stacking Chairs",
    Sales: 700.45
  },
 {
    Order_Date: "2020-10-06",
    Region: "Central",
    Product_Name: "Chairs",
    Sales: 500
  },
  {
    Order_Date: "2020-12-06",
    Region: "East",
    Product_Name: "Self-Adhesive Address Labels for Typewriters by Universal",
    Sales: 14.62
  },
  {
    Order_Date: "2019-11-15",
    Region: "East",
    Product_Name: "Table",
    Sales: 957
  },
  {
    Order_Date: "2019-11-10",
    Region: "East",
    Product_Name: "Eldon Fold",
    Sales: 22
  }
]

预期输出:

[
  {
    Order_Date: "2020-08",
    East: 350,
    South: 993.90,
    Central: 200
  },
  {
    Order_Date: "2020-10",
    East: 0,
    South: 700.45,
    Central: 500
  },
  {
    Order_Date: "2019-11",
    East: 989,
    South: 0,
    Central: 0
  },
  {
    Order_Date: "2020-12",
    East: 14.62,
    South: 0,
    Central: 0
  },
]

我想按年和月 ("YYYY-MM") 对所有 Order_Date 进行分组,并为每个 Order_Date ("YYYY-MM") 组放置每个 Region 值的销售额总和。

我需要像Nivo Bar Chart 数据一样转换数据。但我找不到任何关于转换数据的 nivo 文档。

【问题讨论】:

  • 同一个Order_Date有多个区域时输出应该如何?
  • 是的,我已经更新了预期的输出和原始数据

标签: javascript arrays lodash javascript-objects nivo-react


【解决方案1】:

您可以拥有Order_Date 的动态键,并根据此键对数据进行分组。您可以总结每个区域的 Sales 值以用于重复键。

const rawData=[ { Order_Date: "2020-08-11", Region: "South", Product_Name: "Bookcase", Sales: 261.96 }, { Order_Date: "2020-08-13", Region: "South", Product_Name: "Stacking Chairs", Sales: 731.94 }, { Order_Date: "2020-08-11", Region: "Central", Product_Name: "Table", Sales: 200 }, { Order_Date: "2020-08-11", Region: "East", Product_Name: "Chairs", Sales: 350 }, { Order_Date: "2020-10-06", Region: "South", Product_Name: "Stacking Chairs", Sales: 700.45 }, { Order_Date: "2020-10-06", Region: "Central", Product_Name:"Chairs", Sales: 500 }, { Order_Date: "2020-12-06", Region: "East", Product_Name: "Self-Adhesive Address Labels for Typewriters by Universal", Sales: 14.62 }, { Order_Date: "2019-11-15", Region: "East", Product_Name: "Table", Sales: 957 }, { Order_Date:"2019-11-10", Region: "East", Product_Name: "Eldon Fold", Sales: 22 } ],
      result = Object.values(rawData.reduce((r, {Order_Date, Region, Sales}) => {
        const date = Order_Date.substr(0, 7);
        r[date] ??= {Order_Date: date, East: 0, South: 0, Central: 0};
        r[date][Region] += Sales;
        return r;
      }, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 感谢您的回答。抱歉,我已经更新了问题,预期输出将针对每个地区。
  • @faisal-akbar 更新了答案。
  • 非常感谢。
猜你喜欢
  • 1970-01-01
  • 2023-02-02
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 2021-12-28
  • 2021-01-27
相关资源
最近更新 更多