简体   繁体   English

如何将具有公共属性的 javascript 数组合并为一个?

[英]How to merge javascript array with common properties to a single one?

I have an array of objects with the following structure我有一个具有以下结构的对象数组

[
  [
    {
      exercise: 'Incline Dumbbell Curl',
      weight: 25,
      workload: 600,
      date: '03/23'
    },
    {
      exercise: 'Skullcrushers',
      weight: 25,
      workload: 600,
      date: '03/23'
    }
  ],
  [
    {
      exercise: 'Incline Dumbbell Curl',
      weight: 27.333333333333332,
      workload: 656,
      date: '03/25'
    },
    {
      exercise: 'Skullcrushers',
      weight: 31.333333333333332,
      workload: 752,
      date: '03/25'
    }
  ]
]

And I would like to create an array which looks like the following based on the example above我想根据上面的示例创建一个如下所示的数组

[
  {
    exercise: 'Incline Dumbbell Curl',
    weights: [25, 27.33],
    workloads: [600, 656],
    dates: ['03/23', '03/25']
  },
  {
    exercise: 'Skullcrushers',
    weights: [25, 31.33],
    workloads: [600, 752],
    dates: ['03/23', '03/25']
  }
]

Basically I would like to combine every entry with the same exercise name to create an array with the weights and workloads.基本上,我想将每个条目与相同的练习名称结合起来,以创建一个包含权重和工作负载的数组。 How could I achieve something like that?我怎么能做到这样的事情?

Use forEach to build an object from array.使用forEach从数组构建一个对象。

 const data = [ [ { exercise: "Incline Dumbbell Curl", weight: 25, workload: 600, date: "03/23" }, { exercise: "Skullcrushers", weight: 25, workload: 600, date: "03/23" } ], [ { exercise: "Incline Dumbbell Curl", weight: 27.333333333333332, workload: 656, date: "03/25" }, { exercise: "Skullcrushers", weight: 31.333333333333332, workload: 752, date: "03/25" } ] ]; const update = data => { const arr = data.flat(); const res = {}; arr.forEach(item => { const newItem = res[item.exercise] || { exercise: item.exercise, weight: [], workload: [], date: [] }; ["weight", "workload", "date"].forEach(key => { newItem[key].push(item[key]); }); res[item.exercise] = newItem; }); return Object.values(res); }; console.log(update(data));

You can merge your 2d array into a single array and then use reduce to handle the accumulation process.您可以将二维数组合并为一个数组,然后使用reduce来处理累积过程。

Example:例子:

 const data = [ [ { exercise: 'Incline Dumbbell Curl', weight: 25, workload: 600, date: '03/23' }, { exercise: 'Skullcrushers', weight: 25, workload: 600, date: '03/23' } ], [ { exercise: 'Incline Dumbbell Curl', weight: 27.333333333333332, workload: 656, date: '03/25' }, { exercise: 'Skullcrushers', weight: 31.333333333333332, workload: 752, date: '03/25' } ] ]; // const merged = [ ...data[0], ...data[1] ]; // or const merged = data.reduce( (acc, curr) => ([...curr, ...acc]), []); const results = merged.reduce( (acc, curr) => { const match = acc.find(a => a.exercise === curr.exercise); if (!match) { acc.push({ exercise: curr.exercise, weight: [curr.weight], workload: [curr.workload], date: [curr.date] }); } else { match.weight.push(curr.weight); match.workload.push(curr.workload); match.date.push(curr.date); } return acc; }, []); console.log(results);

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

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