有沒有辦法將不完整的陣列與真實陣列的來源進行比較并回傳缺失的元素?該陣列由物件組成,其中一些物件具有物件陣列。
let incompleteArr =[
{
"name": "Extra Toppings (Small)",
"modifier_options": [{
"name": "Corn",
"price": 0.75
},
{
"name": "Extra mozzarella",
"price": 0.9
},
{
"name": "Onion",
"price": 0.45
}
]
},
{
"name": "Extra Toppings (Large)",
"modifier_options": [{
"name": "Corn",
"price": 1.2
},
{
"name": "Extra Mozzarella",
"price": 1.7
}
]
}
]
}]
let completeArr =[
{
"name": "Extra Toppings (Small)",
"modifier_options": [
{
"name": "Corn",
"price": 0.75
},
{
"name": "Extra mozzarella",
"price": 1.2
},
{
"name": "Extra Anchovies",
"price": 0.7
}
]
},
{
"name": "Extra Toppings (Large)",
"modifier_options": [
{
"name": "Corn",
"price": 1.2
},
{
"name": "Extra mozzarella",
"price": 1.7
}
]
},
{
"name": "Extra Burger Toppings (Small)",
"modifier_options": [
{
"name": "Extra Onion",
"price": 0.5
},
{
"name": "Extra Tomato",
"price": 0.55
},
{
"name": "Extra Cheese",
"price": 0.65
},
{
"name": "Extra Mayo",
"price": 0.3
},
{
"name": "Extra Patty",
"price": 2.5
}
]
},
{
"name": "Extra Burger Toppings (Medium)",
"modifier_options": [
{
"name": "Extra Onion",
"price": 0.6
},
{
"name": "Extra Tomato",
"price": 0.65
},
{
"name": "Extra Cheese",
"price": 0.75
},
{
"name": "Extra Mayo",
"price": 0.4
},
{
"name": "Extra Patty",
"price": 2.9
}
]
},
{
"name": "Extra Burger Toppings (Large)",
"modifier_options": [
{
"name": "Extra Onion",
"price": 0.8
},
{
"name": "Extra Tomato",
"price": 0.9
},
{
"name": "Extra Cheese",
"price": 0.95
},
{
"name": "Extra Mayo",
"price": 0.6
},
{
"name": "Extra Patty",
"price": 3.5
}
]
}
]
期望的結果是回傳一個缺失元素的陣列。例如,“名稱”Extra Toppings (Small) 必須存在于不完整Arr 和完整Arr 中。然后還必須比較“modifier_options”。任何不在不完整Arr 中的modifier_options 都必須推入缺失元素的陣列中。
到目前為止我已經嘗試過了
let missingMod = gfMods.filter(mod => lvMods.every(mod2 => !mod2.name.includes(mod.name)))
預期的輸出是缺少的專案,但我沒有從 modifier_options 物件中獲取缺少的嵌套陣列專案。我有一個想法,我需要遍歷陣列以檢查“名稱”是否存在,然后再進行第二次回圈以檢查modifier_options 是否全部存在。這就是我被卡住的地方。
[{
name:"Extra Burger Toppings (Small)",
modifier_options: [{
name:"Extra Onion",
price:0.5},
{name:"Extra Tomato",
price:0.55},
{name:"Extra Cheese",
price:0.65},
{name:"Extra Mayo",
price:0.3},
{name:"Extra Patty",
price:2.5}
]},
{name:"Extra Burger Toppings (Medium)",
modifier_options: [{
name:"Extra Onion",
price:0.6},
{name:"Extra Tomato",
price:0.65},
{name:"Extra Cheese",
price:0.75},
{name:"Extra Mayo",
price:0.4},
{name:"Extra Patty",
price:2.9}]
},
{name:"Extra Burger Toppings (Large)",
modifier_options: [{
name:"Extra Onion",
price:0.8},
{name:"Extra Tomato",
price:0.9},
{name:"Extra Cheese",
price:0.95},
{name:"Extra Mayo",
price:0.6},
{name:"Extra Patty",
price:3.5}]
}]
uj5u.com熱心網友回復:
該解決方案利用了.reduce(),.find(),.some(),和.filter()陣列的方法。
// Reduces the complete array, to a list of missing values from
// the incomplete array.
const result = completeArr.reduce((prev, next) => {
// Finds whether the object exists in the incomplete array.
// _main will hold the matching object from the incomplete
// array if found, or undefined otherwise.
const _main = incompleteArr.find(obj => obj.name == next.name);
// If it does find, stop here and check for missing
// values inside modifier options.
if(_main){
// .filter to get the missing modifier objects from the
// incomplete array. Done by checking if a
// modifier option of the complete array
// is NOT present in the incomplete array's
// modifier options list.
const _mod = next.modifier_options.filter(next_mod => !_main.modifier_options.some(main_mod => next_mod.name == main_mod.name));
// Add _mod(containing missing modifier options) to
// the accumulating array and return.
return [...prev, {parent: next.name, modifier_options: _mod}];
}
// This next object doesn't exist in the incomplete
// array as it wasn't found, so add it to the accumulating
// list of missing items.
return [...prev, next];
}, []);
let incompleteArr = [
{
"name": "Extra Toppings (Small)",
"modifier_options": [{
"name": "Corn",
"price": 0.75
},
{
"name": "Extra mozzarella",
"price": 0.9
},
{
"name": "Onion",
"price": 0.45
}
]
},
{
"name": "Extra Toppings (Large)",
"modifier_options": [{
"name": "Corn",
"price": 1.2
},
{
"name": "Extra Mozzarella",
"price": 1.7
}
]
}
];
let completeArr = [
{
"name": "Extra Toppings (Small)",
"modifier_options": [
{
"name": "Corn",
"price": 0.75
},
{
"name": "Extra mozzarella",
"price": 1.2
},
{
"name": "Extra Anchovies",
"price": 0.7
}
]
},
{
"name": "Extra Toppings (Large)",
"modifier_options": [
{
"name": "Corn",
"price": 1.2
},
{
"name": "Extra mozzarella",
"price": 1.7
}
]
},
{
"name": "Extra Burger Toppings (Small)",
"modifier_options": [
{
"name": "Extra Onion",
"price": 0.5
},
{
"name": "Extra Tomato",
"price": 0.55
},
{
"name": "Extra Cheese",
"price": 0.65
},
{
"name": "Extra Mayo",
"price": 0.3
},
{
"name": "Extra Patty",
"price": 2.5
}
]
},
{
"name": "Extra Burger Toppings (Medium)",
"modifier_options": [
{
"name": "Extra Onion",
"price": 0.6
},
{
"name": "Extra Tomato",
"price": 0.65
},
{
"name": "Extra Cheese",
"price": 0.75
},
{
"name": "Extra Mayo",
"price": 0.4
},
{
"name": "Extra Patty",
"price": 2.9
}
]
},
{
"name": "Extra Burger Toppings (Large)",
"modifier_options": [
{
"name": "Extra Onion",
"price": 0.8
},
{
"name": "Extra Tomato",
"price": 0.9
},
{
"name": "Extra Cheese",
"price": 0.95
},
{
"name": "Extra Mayo",
"price": 0.6
},
{
"name": "Extra Patty",
"price": 3.5
}
]
}
];
const result = completeArr.reduce((prev, next) => {
const _main = incompleteArr.find(obj => obj.name == next.name);
if(_main){
const _mod = next.modifier_options.filter(next_mod => !_main.modifier_options.some(main_mod => next_mod.name == main_mod.name));
return [...prev, {parent: next.name, modifier_options: _mod}];
}
return [...prev, next];
}, []);
console.log(result);
注意:由于大小寫不同,它將從“Extra Toppings (Large)”回傳“Extra mozzarella”。另外,請原諒可憐的變數名,我希望這個解決方案可以解決您的問題。
更新
根據 OP 的要求,我添加.name了缺失modifier_options所屬的父物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/405612.html
標籤:
