在对象数组中使用reduce-to-sum值会产生错误



我想总结一下如下代码所示的想法:

const bests = [
{
thinking: { _id: '6347d5b4edc3227f056cbc0c', sum: 64, num: 1 },
listening: { _id: '6347d5b4edc3227f056cbc0d', sum: 129, num: 2 },
speaking: { _id: '6347d5b4edc3227f056cbc0e', sum: 0, num: 2 },
writing: { _id: '6347d5b4edc3227f056cbc0f', sum: 0, num: 0 },
speed: { _id: '6347d5b4edc3227f056cbc10', sum: 187, num: 2 },
overall: { _id: '6347d5b4edc3227f056cbc11', sum: 129, num: 2 }
},
{
thinking: { _id: '6347e46665609042876fc4a6', sum: 100, num: 1 },
listening: { _id: '6347e46665609042876fc4a7', sum: 100, num: 1 },
speaking: { _id: '6347e46665609042876fc4a8', sum: 100, num: 1 },
writing: { _id: '6347e46665609042876fc4a9', sum: 0, num: 0 },
speed: { _id: '6347e46665609042876fc4aa', sum: 88, num: 1 },
overall: { _id: '6347e46665609042876fc4ab', sum: 100, num: 1 }
},
{
thinking: { _id: '6347e54f65609042876fc4af', sum: 10, num: 10 },
listening: { _id: '6347e54f65609042876fc4b0', sum: 65, num: 1 },
speaking: { _id: '6347e54f65609042876fc4b1', sum: 0, num: 1 },
writing: { _id: '6347e54f65609042876fc4b2', sum: 0, num: 0 },
speed: { _id: '6347e54f65609042876fc4b3', sum: 95, num: 1 },
overall: { _id: '6347e54f65609042876fc4b4', sum: 65, num: 1 }
}
]



const sum = bests.reduce((a, b) => a['thinking'].sum + b['thinking'].sum);
console.log('sum::', sum);

但正如你所看到的,我得到了有线错误!我该怎么解决这个问题?

reduce()的第一个参数是累加器,不需要['thinking]

传递0作为初始值,因此累加器变成一个整数,因此我们可以将b['thinking'].sum:相加

bests.reduce((a, b) => a + b['thinking'].sum, 0);

const bests = [{thinking: { _id: '6347d5b4edc3227f056cbc0c', sum: 64, num: 1 }, listening: { _id: '6347d5b4edc3227f056cbc0d', sum: 129, num: 2 }, speaking: { _id: '6347d5b4edc3227f056cbc0e', sum: 0, num: 2 }, writing: { _id: '6347d5b4edc3227f056cbc0f', sum: 0, num: 0 }, speed: { _id: '6347d5b4edc3227f056cbc10', sum: 187, num: 2 }, overall: { _id: '6347d5b4edc3227f056cbc11', sum: 129, num: 2 } }, {thinking: { _id: '6347e46665609042876fc4a6', sum: 100, num: 1 }, listening: { _id: '6347e46665609042876fc4a7', sum: 100, num: 1 }, speaking: { _id: '6347e46665609042876fc4a8', sum: 100, num: 1 }, writing: { _id: '6347e46665609042876fc4a9', sum: 0, num: 0 }, speed: { _id: '6347e46665609042876fc4aa', sum: 88, num: 1 }, overall: { _id: '6347e46665609042876fc4ab', sum: 100, num: 1 } }, {thinking: { _id: '6347e54f65609042876fc4af', sum: 10, num: 10 }, listening: { _id: '6347e54f65609042876fc4b0', sum: 65, num: 1 }, speaking: { _id: '6347e54f65609042876fc4b1', sum: 0, num: 1 }, writing: { _id: '6347e54f65609042876fc4b2', sum: 0, num: 0 }, speed: { _id: '6347e54f65609042876fc4b3', sum: 95, num: 1 }, overall: { _id: '6347e54f65609042876fc4b4', sum: 65, num: 1 } } ];
const sum = bests.reduce((a, b) => a + b['thinking'].sum, 0);
console.log('sum::', sum);

下面的代码提供了预期的结果。

const bests = [
{
thinking: { _id: '6347d5b4edc3227f056cbc0c', sum: 64, num: 1 },
listening: { _id: '6347d5b4edc3227f056cbc0d', sum: 129, num: 2 },
speaking: { _id: '6347d5b4edc3227f056cbc0e', sum: 0, num: 2 },
writing: { _id: '6347d5b4edc3227f056cbc0f', sum: 0, num: 0 },
speed: { _id: '6347d5b4edc3227f056cbc10', sum: 187, num: 2 },
overall: { _id: '6347d5b4edc3227f056cbc11', sum: 129, num: 2 }
},
{
thinking: { _id: '6347e46665609042876fc4a6', sum: 100, num: 1 },
listening: { _id: '6347e46665609042876fc4a7', sum: 100, num: 1 },
speaking: { _id: '6347e46665609042876fc4a8', sum: 100, num: 1 },
writing: { _id: '6347e46665609042876fc4a9', sum: 0, num: 0 },
speed: { _id: '6347e46665609042876fc4aa', sum: 88, num: 1 },
overall: { _id: '6347e46665609042876fc4ab', sum: 100, num: 1 }
},
{
thinking: { _id: '6347e54f65609042876fc4af', sum: 10, num: 10 },
listening: { _id: '6347e54f65609042876fc4b0', sum: 65, num: 1 },
speaking: { _id: '6347e54f65609042876fc4b1', sum: 0, num: 1 },
writing: { _id: '6347e54f65609042876fc4b2', sum: 0, num: 0 },
speed: { _id: '6347e54f65609042876fc4b3', sum: 95, num: 1 },
overall: { _id: '6347e54f65609042876fc4b4', sum: 65, num: 1 }
}
]

// changed code block start
const sum = bests.reduce((a, b) => a + b['thinking'].sum,0);
// changed code block end
console.log('sum::', sum);

说明:a是加上当前值之前所有值的总和。因此,它是一个数字,而不是该列表中的项目,并且调用a['thinking'].sum会导致错误,因为a是一个号码,因此a['thinking']是未定义的。b是列表的当前项。此外,您还必须传递初始值0。

相关内容

最新更新