【发布时间】:2019-12-03 11:17:19
【问题描述】:
我正在尝试使用 reduce 在 JSON Array 中求和。以及我得到的返回值NAN
请任何人帮我检查我的代码是否正确。
她是我的密码
const reduceBids = [ { id: 'd4f82532-d38a-421f-81d5-795dcfc7cd70',
vehicleId: '20f3f453-774c-4e4e-9dd6-a3b3468e42f4',
userId: '0d7a45e7-3483-4cc6-95b0-12e551002a7a',
bidAmount: 300,
fullName: 'Hanuman' },
{ id: 'f4631745-7181-4d9c-92ce-948751fa7e14',
vehicleId: '20f3f453-774c-4e4e-9dd6-a3b3468e42f4',
userId: 'bc9b172d-b8a2-48bd-8605-a13e9536dc00',
bidAmount: 300,
fullName: 'Venkatesh' },
{ id: '44422594-e69d-48cb-b8d1-7493b5275e26',
vehicleId: '20f3f453-774c-4e4e-9dd6-a3b3468e42f4',
userId: '0d7a45e7-3483-4cc6-95b0-12e551002a7a',
bidAmount: 300,
fullName: 'Hanuman' },
{ id: '747cb11d-1306-4638-b90e-cc197c3648e7',
vehicleId: '20f3f453-774c-4e4e-9dd6-a3b3468e42f4',
userId: '0d7a45e7-3483-4cc6-95b0-12e551002a7a',
bidAmount: 300,
fullName: 'Hanuman' },
{ id: '879364ba-d672-4ad9-a4b8-2cc29c3c912a',
vehicleId: '20f3f453-774c-4e4e-9dd6-a3b3468e42f4',
userId: '0d7a45e7-3483-4cc6-95b0-12e551002a7a',
bidAmount: 300,
fullName: 'Hanuman' },
{ id: '9bbb3ee3-7f27-4743-aa3e-553e399dce9a',
vehicleId: '20f3f453-774c-4e4e-9dd6-a3b3468e42f4',
userId: '0d7a45e7-3483-4cc6-95b0-12e551002a7a',
bidAmount: 300,
fullName: 'Hanuman' },
{ id: '4a7026c3-0688-42a7-bfe7-cbb733dcb656',
vehicleId: '20f3f453-774c-4e4e-9dd6-a3b3468e42f4',
userId: '0d7a45e7-3483-4cc6-95b0-12e551002a7a',
bidAmount: 300,
fullName: 'Hanuman' },
{ id: '7918cb18-0951-40b9-a687-7bb4a03d5bec',
vehicleId: '20f3f453-774c-4e4e-9dd6-a3b3468e42f4',
userId: '0d7a45e7-3483-4cc6-95b0-12e551002a7a',
bidAmount: 300,
fullName: 'Hanuman' },
{ id: 'b94b4f57-41c2-442e-85ae-d3ddceaaa985',
vehicleId: '20f3f453-774c-4e4e-9dd6-a3b3468e42f4',
userId: '0d7a45e7-3483-4cc6-95b0-12e551002a7a',
bidAmount: 300,
fullName: 'Hanuman' },
{ id: 'acfff167-3b71-4f60-a229-95b5444e25e9',
vehicleId: '20f3f453-774c-4e4e-9dd6-a3b3468e42f4',
userId: '0d7a45e7-3483-4cc6-95b0-12e551002a7a',
bidAmount: 200,
fullName: 'Hanuman' } ];
const total = reduceBids.reduce((bid, currentBid) => {
return bid.bidAmount + currentBid.bidAmount;
});
console.log(total);
我需要计算bidAmount 的总金额,而不是总数。
我收到NAN 请运行代码并检查输出
我们将不胜感激。
【问题讨论】:
标签: javascript node.js reduce