简体   繁体   English

基于 2 Arrays 创建对象数组

[英]Create Array of Objects Based on 2 Arrays

I have an array, it looks like this.我有一个数组,它看起来像这样。

const date = ['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05']

I have an array of objects, it looks like this.我有一个对象数组,它看起来像这样。

const data = [
  {
    category: 'a',
    date: '2021-01-01',
    qty: '1'
  },
  {
    category: 'a',
    date: '2021-01-02',
    qty: '2'
  },
  {
    category: 'b',
    date: '2021-01-02',
    qty: '1'
  },
  {
    category: 'b',
    date: '2021-01-03',
    qty: '2'
  },
  {
    category: 'c',
    date: '2021-01-03',
    qty: '1'
  },
  {
    category: 'c',
    date: '2021-01-04',
    qty: '2'
  },
]

I want the result to be like this, the datasets's length should be 5 (based on date's length).我希望结果是这样的,数据集的长度应该是 5(基于日期的长度)。

[
  {
    label: 'a',
    datasets: ['1', '2', '0', '0', '0']
  },
  {
    label: 'b',
    datasets: ['0', '1', '2', '0', '0']
  },
  {
    label: 'c',
    datasets: ['0', '0', '1', '2', '0']
  },
]

I tried code by myself, but the result looks like this我自己尝试了代码,但结果看起来像这样

[
  {
    label: 'a',
    datasets: ['1', '2']
  },
  {
    label: 'b',
    datasets: ['1', '2']
  },
  {
    label: 'c',
    datasets: ['1', '2']
  },
]

Can anyone help me to code this?任何人都可以帮我编码吗?

Edit编辑

 const data = [ { category: 'a', date: '2021-01-01', qty: '1' }, { category: 'a', date: '2021-01-02', qty: '2' }, { category: 'b', date: '2021-01-02', qty: '1' }, { category: 'b', date: '2021-01-03', qty: '2' }, { category: 'c', date: '2021-01-03', qty: '1' }, { category: 'c', date: '2021-01-04', qty: '2' }, ]; var output = []; data.forEach(function(item) { var existing = output.filter(function(v, i) { return v.label == item.category; }); if (existing.length) { var existingIndex = output.indexOf(existing[0]); output[existingIndex].datasets = output[existingIndex].datasets.concat(item.qty); } else { if (typeof item.qty == 'string') item.qty = [item.qty]; output.push({ label: item.category, datasets: item.qty }); } }); console.log('Output', output);

It looks like, you do not respect the lenth of the dat array and just push the value to the datasets of the group.看起来,您不尊重 dat 数组的长度,只是将值push送到组的datasets

To overcome this, you could take an object for getting the index of the date and for not given datasets , map date with value zero.为了克服这个问题,您可以使用 object 来获取日期的索引,对于未给定的datasets ,map date的值为零。

 const dates = ['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05'], data = [{ category: 'a', date: '2021-01-01', qty: '1' }, { category: 'a', date: '2021-01-02', qty: '2' }, { category: 'b', date: '2021-01-02', qty: '1' }, { category: 'b', date: '2021-01-03', qty: '2' }, { category: 'c', date: '2021-01-03', qty: '1' }, { category: 'c', date: '2021-01-04', qty: '2' }], indices = Object.fromEntries(dates.map((k, i) => [k, i])), result = Object.values(data.reduce((groups, { category: label, date, qty }) => { groups[label]??= { label, datasets: Array.from(dates).fill('0') }; groups[label].datasets[indices[date]] = qty; return groups; }, {})); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

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

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