我尝试在使用array.reduce时访问数组项。 item.ay属性是动态的。我必须陷入困境。但是我无法访问该项目。这是我的代码
objectKeys.forEach(ay => {
let lastArray = this.personalDataset.reduce((a, item, index) => {
return (
a + (this.spendingGroups[index].yourRate / 100) * (1 + item.ay)
);
}, 0);
chartArray.push((lastArray - 1) * 100);
});
console.log(chartArray);
此代码返回[nan,nan]数组。当使用reduce无循环并将其全部推入时。
尝试通过添加加号Number
使用隐式转换为+
:
objectKeys.forEach(ay => {
let lastArray = this.personalDataset.reduce((a, item, index) => {
return (
a + (this.spendingGroups[index].yourRate / 100) * (1 + (+item[ay]))
);
}, 0);
chartArray.push((lastArray - 1) * 100);
});
console.log(chartArray);