Is there a way to compare an incomplete array to the source of truth array & return the missing elements? The array consists of objects & some of the objects have an array of objects.
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
}
]
}
]
Desired result is to return an array of missing elements. For example the "name" Extra Toppings (Small) must exist in the incompleteArr & the completeArr. Then the "modifier_options" must also be compared. Any modifier_options not in the incompleteArr must be pushed into the array of missing elements.
I have tried this so far
let missingMod = gfMods.filter(mod => lvMods.every(mod2 => !mod2.name.includes(mod.name)))
The output as expected is the missing items BUT I am not getting the missing nested array items from the modifier_options object. I have an idea that I need to loop through the arrays checking that the "names" exist & then a second loop to check if the modifier_options all exist. This is where I have been stuck.
[{
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}]
}]
lvModsandgfModsrefer to respectively?