【发布时间】:2020-01-08 07:02:20
【问题描述】:
我正在尝试将新生成的对象推送到 JavaScript 中的数组中
我有一个表单数组:
savings: any = [{month: "January", amount: 300}, {month: "February", amount:450}, {month: "March", amount: 500}]
以及一个名为 savings_bf 的变量;
savings_bf = 15000
我使用数组 savings 和 savings_bf 中的 amount 值来计算运行总计,使用此处提供的解决方案 https://stackoverflow.com/a/57737436/11849137获取输出:
_total_cf = [15300,15750,16250]
我的代码如下所示;
savings.forEach(s =>s.total_cf = _total_cf)
新 savings 数组的预期输出应该是:
[{month: "January", amount: 300, total_cf: 15300 }, {month: "February", amount:450, total_cf: 15750}, {month: "March", amount: 500 total_cf: 16250}]
实际输出:
[{month: "January", amount: 300, total_cf: [15300,15750,16250]}, {month: "February", amount:450, total_cf: [15300,15750,16250]}, {month: "March", amount: 500 total_cf: [15300,15750,16250]}]
【问题讨论】:
标签: javascript html arrays typescript