我有一個對象數組的數組更新值如下:查找和嵌套對象
products = [
{
id: 1,
title: "Product 1",
specifications: {
price: 1.55,
discount: 15,
attributes: [
{
l1: 100,
l2: 80
height:200,
weight: 15,
parameters: [
{
id: 199199 // this is how I identify the parameter
size: 185 // this is what I want to change
}, ...
]
}, ...
]
}
}, ...
]
...和改變我想申請的參數數組,例如:改變大小,其中189 product.specifications.attributes.parameters.id == 199199.
我想這樣做而不扁平化任何元素,因爲它們是Vue.js數據結構的一部分,它會打破反應性。
我該怎麼做?我願意使用下劃線或下劃線
非常好。由於參數ID是唯一的,我應該在更改大小後中斷嗎?根據http://stackoverflow.com/questions/183161/best-way-to-break-from-nested-loops-in-javascript –
在屏幕上對5000個產品進行測試,我選擇了你的答案,因爲它看起來像與stasovlas提交的數據相比,我只需要幾個ms來更新所有的數據。嘗試在更新後打破週期,這將節省更多的週期,因爲產品只會顯示一次。 –
@NickM不幸的是,Array.prototype.map沒有內置的中斷選項。除非你有大量的項目,否則你在性能上可能看不到太大的差異。 – crabbly