【问题标题】:How to merge javascript array with common properties to a single one?如何将具有公共属性的javascript数组合并为一个数组?
【发布时间】:2020-07-06 06:25:12
【问题描述】:

我有一个具有以下结构的对象数组

[
  [
    {
      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'
    }
  ]
]

我想根据上面的例子创建一个如下所示的数组

[
  {
    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']
  }
]

基本上,我想将每个具有相同练习名称的条目组合起来,以创建一个包含权重和工作负载的数组。 我怎么能做到这样?

【问题讨论】:

  • 你考虑过Array.reduce()吗?
  • 我怎么能这样做呢?你能帮忙吗?
  • 您想按exercise 类型对它们进行分组。只需在 SO 上搜索“javascript array group by”。会有一堆答案。

标签: javascript arrays node.js json javascript-objects


【解决方案1】:

您可以将二维数组合并为一个数组,然后使用reduce 处理累积过程。

例子:

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);

【讨论】:

    【解决方案2】:

    使用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));

    【讨论】:

      猜你喜欢
      • 2019-06-11
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      相关资源
      最近更新 更多