I've two arrays of objects:
var arr1 = [{
"id": "id_1",
"name": "Skoda"
}, {
"id": "id_2",
"name": "BMW"
}];
var arr2 = [{
"id": "id_1",
"name": "Skoda - Auto"
}, {
"id": "id_3",
"name": "BMW - Auto"
},{
"id": "id_4",
"name": "Merc - Auto"
}]
Output
var arr1 = [{
"id": "id_3",
"name": "BMW - Auto"
},{
"id": "id_4",
"name": "Merc - Auto"
},{
"id": "id_1",
"name": "Skoda"
}, {
"id": "id_2",
"name": "BMW"
}];
I want to merge two arrays arr1 & arr2, basically I want to unshift the object from arr2 to the arr1 only if the Id is not present in arr1, Hence we're not adding id_1
to the arr1. I'm not sure how can we achieve it
I tried the below way but it keeps on adding duplicate items, how can I avoid duplication of id
arr1.unshift.apply(arr1, arr2)