为什么 .reduce() 要求我在使用它对对象中的特定键值对求和时输入累加器的起始值



我想我实际上可能已经想通了这一点,只是在寻找确认以确保我真的理解这一点。

我正在用这个数组做练习

const cards = [
{ id:4, name:'charmander', type:'fire', base_experience:62 },
{ id:7, name:'Squirtle', type:'water', base_experience:63 },
{ id:11, name:'Metapod', type:'bug', base_experience:72 },
{ id:12, name:'Butterfree', type:'flying', base_experience:178 },
{ id:25, name:'Pikachu', type:'electric', base_experience:112 },
{ id:39, name:'Jigglypuff', type:'normal', base_experience:95 },
{ id:94, name:'Gengar', type:'poison', base_experience:225 },
{ id:133, name:'Eevee', type:'normal', base_experience:65 },
]

这减少了

const totalScore = cards.reduce((acc, card) => {
return acc + card.base_experience
})

它不断返回一个奇怪的对象之类的东西,并连接字符串而不是汇总数字......我认为因为我没有为累加器提供起始值,它只是使用第一个对象作为累加器,然后尝试向其添加一个数字,但它只是不起作用。一旦我添加了这样的起始值。

const totalScore = cards.reduce((acc, card) => {
return acc + card.base_experience
}, 0)

它工作正常。我想知道是否有更好的方法来做到这一点。我试过这个。

const totalScore = cards.reduce((acc, card) => {
return acc.base_experience + card.base_experience
})

我几乎知道它会失败,因为第二次通过 acc 会很古怪。

任何想法将不胜感激。

来自 MDN:

initialValue:用作回调第一次调用的第一个参数的值。如果未提供 initialValue,则将使用并跳过数组中的第一个元素。

在您的情况下,您必须添加一个初始值,因为您要处理数字。您可以压缩更多的代码(因为您只返回一个值(:

const cards = [
{id: 4, name: "charmander", type: "fire", base_experience: 62},
{id: 7, name: "Squirtle", type: "water", base_experience: 63},
{id: 11, name: "Metapod", type: "bug", base_experience: 72},
{id: 12, name: "Butterfree", type: "flying", base_experience: 178},
{id: 25, name: "Pikachu", type: "electric", base_experience: 112},
{id: 39, name: "Jigglypuff", type: "normal", base_experience: 95},
{id: 94, name: "Gengar", type: "poison", base_experience: 225},
{id: 133, name: "Eevee", type: "normal", base_experience: 65},
]
const totalScore = cards.reduce((acc, {base_experience}) =>
acc + base_experience
, 0)
console.log(totalScore)

您也可以先映射base_experience,然后进行求和:

const cards = [
{id: 4, name: "charmander", type: "fire", base_experience: 62},
{id: 7, name: "Squirtle", type: "water", base_experience: 63},
{id: 11, name: "Metapod", type: "bug", base_experience: 72},
{id: 12, name: "Butterfree", type: "flying", base_experience: 178},
{id: 25, name: "Pikachu", type: "electric", base_experience: 112},
{id: 39, name: "Jigglypuff", type: "normal", base_experience: 95},
{id: 94, name: "Gengar", type: "poison", base_experience: 225},
{id: 133, name: "Eevee", type: "normal", base_experience: 65},
]
const totalScore = cards
.map(c => c.base_experience)
.reduce((a, b) => a + b, 0)
console.log(totalScore)

我能提出的最后一个是sumBy:

const cards = [
{id: 4, name: "charmander", type: "fire", base_experience: 62},
{id: 7, name: "Squirtle", type: "water", base_experience: 63},
{id: 11, name: "Metapod", type: "bug", base_experience: 72},
{id: 12, name: "Butterfree", type: "flying", base_experience: 178},
{id: 25, name: "Pikachu", type: "electric", base_experience: 112},
{id: 39, name: "Jigglypuff", type: "normal", base_experience: 95},
{id: 94, name: "Gengar", type: "poison", base_experience: 225},
{id: 133, name: "Eevee", type: "normal", base_experience: 65},
]
const totalScore = _.sumBy(cards, "base_experience")
console.log(totalScore)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

你可以做这样的事情:

const cards = [
{ id:4, name:'charmander', type:'fire', base_experience:62 },
{ id:7, name:'Squirtle', type:'water', base_experience:63 },
{ id:11, name:'Metapod', type:'bug', base_experience:72 },
{ id:12, name:'Butterfree', type:'flying', base_experience:178 },
{ id:25, name:'Pikachu', type:'electric', base_experience:112 },
{ id:39, name:'Jigglypuff', type:'normal', base_experience:95 },
{ id:94, name:'Gengar', type:'poison', base_experience:225 },
{ id:133, name:'Eevee', type:'normal', base_experience:65 },
]
const totalScore = cards
.map(s => s.base_experience)
.reduce((acc, exp) => acc + exp)

console.dir(totalScore)

最新更新