您可以使用reduce()函数对属性进行累加,并返回一个新的对象。下面是一个示例代码:

const collectors = [
  {
    name: 'John',
    amount: 100
  },
  {
    name: 'Alice',
    amount: 200
  },
  {
    name: 'Bob',
    amount: 300
  }
];

const reducedCollector = collectors.reduce((accumulator, collector) => {
  return {
    name: '',
    amount: accumulator.amount + collector.amount
  };
});

console.log(reducedCollector);

输出结果将是:

{
  name: '',
  amount: 600
}

在这个示例中,我们使用reduce()函数将collectors数组中的amount属性进行累加,并将结果存储在reducedCollector对象的amount属性中

Collectorsreducing 对象属性进行累加 返回对象

原文地址: https://www.cveoy.top/t/topic/iZfF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录