l want to push object array into the main array. l want to check if that object array is already exist don't push . My code below he is pushing objects although are already exists.
add_addon(addon, title, index) {
if (this.items_local[index].addons) {
for (let i = 0; i < this.items_local[index].addons.length; i++) {
const element = this.items_local[index].addons[i];
if (element.title === addon.title) {
console.log(element.title === addon.title);
alert('item is exist');
} else {
this.items_local[index].addons.push(addon);
}
}
}
}
output :
[
{
"size": true,
"desc": "صحن مخلمة",
"ratting": 0,
"quantiy": 1,
"name": "مخلمة",
"id": "QmzeJgg2F2",
"price": 500,
"addons": [
{
"title": "لحم بعجين ",
"price": 2
},
{
"title": "خبز بعجين",
"price": 0.1
},
{
"title": "لحم بعجين ",
"price": 2
},
{
"title": "خبز بعجين",
"price": 0.1
},
{
"title": "خبز بعجين",
"price": 0.1
}
]
}
]
How to prevent pushing objects array if already exist?