【问题标题】:Transform JavaScript array of object in long format for React-table为 React-table 转换长格式的 JavaScript 对象数组
【发布时间】:2021-10-06 09:22:54
【问题描述】:

如何将宽格式的对象数组转换为长格式

输入数组:

[
  {
    series: "opening_balance",
    "Oct 2021": 12713238.3,
    "Nov 2021": 27713238.3,
    "Dec 2021": 22713238.3,
  },
  {
    series: "inflow",
    "Oct 2021": 7,
    "Nov 2021": 40000000,
    "Dec 2021": 50000000,
  }
];

输出数组:

[
  { year_month: "Oct 2021", opening_balance: 5, inflow: 100 },
  { year_month: "Nov 2021", opening_balance: 10, inflow: 200 },
  { year_month: "Dec 2021", opening_balance: 15, inflow: 150 },
];

输出也是来自 API 调用的原始数据。它被转换为用于 React-table 的宽格式,并且可以正常工作。

但是,表格单元格现在是可编辑的,它需要更新另一个组件中的源数据;因此,需要获取原始格式。

我尝试了多种方法,但都无法成功。以下是不工作的代码

let data = [
  {
    series: "opening_balance",
    "Oct 2021": 5,
    "Nov 2021": 10,
    "Dec 2021": 15,
  },
  {
    series: "inflow",
    "Oct 2021": 100,
    "Nov 2021": 200,
    "Dec 2021": 150,
  },
];

let results = data.map((row) => {
  let keys = Object.keys(row);

  let x = keys.map((key) => {
    return { year_month: key, value: row[key] };
  });
  return [...x];
});

console.log("results:", results);

生产

results: [
  [
    { year_month: 'series', value: 'opening_balance' },
    { year_month: 'Oct 2021', value: 5 },
    { year_month: 'Nov 2021', value: 10 },
    { year_month: 'Dec 2021', value: 15 }
  ],
  [
    { year_month: 'series', value: 'inflow' },
    { year_month: 'Oct 2021', value: 100 },
    { year_month: 'Nov 2021', value: 200 },
    { year_month: 'Dec 2021', value: 150 }
  ]
]

有人可以帮忙转换数据吗?

【问题讨论】:

    标签: javascript arrays react-table-v7


    【解决方案1】:

    我对你的建议是在后端进行转换,可以在数据库层或 API 层。

    虽然转换可以这样艰难地完成: https://stackblitz.com/edit/node-8u47y6?file=index.js

        const arr = [
          {
            series: 'opening_balance',
            'Oct 2021': 12713238.3,
            'Nov 2021': 27713238.3,
            'Dec 2021': 22713238.3,
          },
          {
            series: 'inflow',
            'Oct 2021': 7,
            'Nov 2021': 40000000,
            'Dec 2021': 50000000,
          },
          {
            series: 'customXYZ',
            'Oct 2021': 1,
            'Nov 2021': 2,
            'Dec 2021': 3,
          },
        ];
    
        const hashMap = {};
    
        // Creating the hashmap for each year_month
        arr.forEach((obj) => {
          const series = obj.series;
          Object.entries(obj).forEach(([key, value]) => {
            if (key !== 'series') {
              if (!hashMap[key]) {
                hashMap[key] = {};
              }
              hashMap[key][series] = value;
            }
          });
        });
    
        // transforming the same to array like values
        const result = Object.entries(hashMap).map(([key, value]) => ({
          year_month: key,
          ...value,
        }));
    
        console.log(result);
    

    【讨论】:

    • 谢谢!有效。 API 由另一个团队制作,通常采用简单的长格式。我可以尝试更改 API 本身,而不是在 JavaScript 中转换数据。此外,如果 react-table 可以采用简单数据框类型的数据或关系数据库的保存方式将有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 2020-10-18
    • 1970-01-01
    • 2018-07-14
    • 2021-12-28
    • 2019-10-22
    • 1970-01-01
    相关资源
    最近更新 更多