gpt4 book ai didi

javascript - 减少数组内部的数组

转载 作者:行者123 更新时间:2023-12-05 09:04:40 25 4
gpt4 key购买 nike

所以我有这个数组:

const filteredArray2 = [
[{
'item': "Chocolate Waffle",
'price': 5000
},
{
'item': "Strawberry Waffle",
'price': 6000
}
],
[{
'item': "Chocolate Waffle",
'price': 5000
}]
];

我想要总计 16000,我所知道的是我可以使用 array.reduce,但我不知道如何使用它。

最佳答案

可以先flat()多维数组,再归约

const filteredArray = [
[{ item: "Chocolate Waffle", price: 5000},{item: "Strawberry Waffle", price: 6000}],
[{ item: "Chocolate Waffle", price: 5000}]]
let total = filteredArray.flat().reduce((b, a) => b + +a.price, 0);
console.log(total);

关于javascript - 减少数组内部的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68412582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com