如何将 javascript 中的嵌套对象数组合并为特定形状

How to merge a nested array of objects in javascript with a specific shape

我正在尝试通过对总计对象下每个键值对的总计求和来合并一个对象数组。例如,下面的数组将生成一个对象,其中 totals 个对象包含 3 个苹果和 5 个橙子。这应该是动态的。如果 pears 是另一个对象中的键,则生成的对象将在 totals 对象下包含三个键:apples、oranges 和 pears。

示例输入:

[
  {
    summary: {
      totals: {
        apples: 2,
        oranges: 3
      }
    }
  },
  {
    summary: {
      totals: {
        apples: 1,
        oranges: 2
      }
    }
  }
]

预期输出:

{
  summary:{
    totals:{
      apples:3,
      oranges:5
    }
  }
}

我试过的:

function mergeObjects(arr) {
  let shape = {
    summary:{
      totals:{}
    }
  }
  
  return arr.reduce((prev, cur) => {
   if(cur.summary.totals.apples){
     shape.summary.totals.apples.push(cur.summary.totals.apples)
   }
  }, shape);
}
  • 使用Array#reduce,在更新对象时迭代数组
  • 在每次迭代中,使用 Object#entries ,迭代当前总计对并更新累加器。

const arr = [
  { summary: { totals: { apples: 2, oranges: 3 } } },
  { summary: { totals: { apples: 1, oranges: 2 } } },
];

const res = arr.reduce((map, current) => {
  const { totals: currentTotals = {} } = current.summary ?? {};
  const { totals } = map.summary;
  Object.entries(currentTotals).forEach(([ key, value ]) => {
    totals[key] = (totals[key] ?? 0) + value;
  });
  return map;
}, { summary: { totals: {} } });

console.log(res);

你可以尝试类似的东西。只需遍历数组并总结苹果和橘子。

const arr = [
  {
    summary: {
      totals: {
        apples: 2,
        oranges: 3,
      },
    },
  },
  {
    summary: {
      totals: {
        apples: 1,
        oranges: 2,
      },
    },
  },
];

function mergeObjects(arr) {
  let shape = {
    summary:{
      totals:{
      apples:0,
      oranges:0
      }
    }
  }
  
  arr.forEach(x => {
   if(x.summary.totals.apples){
     shape.summary.totals.apples += x.summary.totals.apples;
     shape.summary.totals.oranges += x.summary.totals.oranges;
   }
  });
  return shape;
}

let result = mergeObjects(arr);
console.log(result);

reduce 函数的第二个选项初始化值。

并且初始化值可以在prev中使用!

[1]将值存入prev.

[2] prev可以累加值。您必须 return 才能使用累积值。如果没有 returned,该值将是未定义的。

[3]apples不是数组类型,所以不能使用push方法。它是一个数字类型,你必须使用数字运算符。

function mergeObjects(arr) {
  const shape = {
    summary: {
      totals: {},
    },
  };

  return arr.reduce((prev, cur) => {
    const { apples, oranges } = cur.summary.totals;
    // [1]
    prev.summary.totals.apples
      // [3]
      ? (prev.summary.totals.apples += apples)
      : (prev.summary.totals.apples = apples);
    prev.summary.totals.oranges
      ? (prev.summary.totals.oranges += oranges)
      : (prev.summary.totals.oranges = oranges);
    // [2]
    return prev;
  }, shape);
}

小贴士!​​

  1. 使用解构赋值
const { apples, oranges } = cur.summary.totals;
  1. 使用三元运算符
prev.summary.totals.apples
      ? (prev.summary.totals.apples += apples)
      : (prev.summary.totals.apples = apples);
  • 让代码看起来更漂亮!

你可以合并Object.entries() with Array#reduce() and Array.forEach()

代码:

const data = [{summary: {totals: {apples: 2,oranges: 3}}},{summary: {totals: {apples: 1,oranges: 2}}}]

const result = data.reduce((a, c) => {
  Object
    .entries(c.summary.totals)
    .forEach(([k, v]) => a.summary.totals[k] += v)
  return a
},
{ summary: { totals: { apples: 0, oranges: 0 } } })

console.log(result)