我使用过滤器来查找嵌套数组中的所有项目。但是,我想更改所有找到的项目的“数量”。同一项目可以在多个类别和子类别中找到。
如果能这样修改它会很好,但是它不起作用...
let item_id = 681
let new_quantity = 10
categories.flatMap(category => category.subcategories.flatMap(subcategory => subcategory.items))
.filter(item => item.id == item_id)
.item.quantity = new_quantity
这是我的类别>子类别>项目数组(我想将数量从5更改为10,其中item_id = 681)
[
{
id: 1,description: 'First category',subcategories: [
{
id: 1,description: 'First subcategory',items: [
{
id: 681,description: 'House',quantity: 5,}
]
}
]
},{
id: 2,description: 'Second category',]