I am trying to merge two array of object as per key value pair but its giving the result which is not as expected. I am explaining all the document with my code below.
let arr1 = [
{
"_id": "PU2-TEXT BOOK",
"dispatchcount": 2,
"totalDispatchValue": 5810,
"totalDiscountValue": 150
},
{
"_id": "PU2-Mathematics Part - 1 Text Book",
"dispatchcount": 1,
"totalDispatchValue": 4131,
"totalDiscountValue": 150
},
{
"_id": "Boys White & Blue Striped Half Shirt",
"dispatchcount": 1,
"totalDispatchValue": 4131,
"totalDiscountValue": 150
}
]
let arr2 = [
{
"_id": "PU2-TEXT BOOK",
"pendingcount": 2,
"totalPendingValue": 14157,
"totalPendingDiscountValue": 1518
},
{
"_id": "PU2-Accountancy Part - 2 Text Book",
"pendingcount": 1,
"totalPendingValue": 9002,
"totalPendingDiscountValue": 834
}
]
let arr3 = arr1.map((item, i) => Object.assign({}, item, arr2[i]));
console.log(arr3);
Here I need to merge both arr1 and arr2 as per key _id value if any matches found then the both object will merge if not found from 2nd array then original value will remain as it is. My expected output is given below.
Expected output::
[
{
"_id": "PU2-TEXT BOOK" ,
"dispatchcount": 2 ,
"totalDispatchValue": 5810 ,
"totalDiscountValue": 150 ,
"pendingcount": 2 ,
"totalPendingValue": 14157 ,
"totalPendingDiscountValue": 1518
},
{
"_id": "PU2-Mathematics Part - 1 Text Book",
"dispatchcount": 1,
"totalDispatchValue": 4131,
"totalDiscountValue": 150,
"pendingcount": 0 ,
"totalPendingValue": 0 ,
"totalPendingDiscountValue": 0
},
{
"_id": "Boys White & Blue Striped Half Shirt",
"dispatchcount": 1,
"totalDispatchValue": 4131,
"totalDiscountValue": 150,
"pendingcount": 0 ,
"totalPendingValue": 0 ,
"totalPendingDiscountValue": 0
},
{
"_id": "PU2-Accountancy Part - 2 Text Book",
"pendingcount": 1,
"totalPendingValue": 9002,
"totalPendingDiscountValue": 834,
"dispatchcount": 0 ,
"totalDispatchValue": 0 ,
"totalDiscountValue": 0
}
]
So here i want merge two array of object as per _id key. If some where its matched then both object will merge and if object from 1st array is not present in 2nd or vise versa also it will merge with value 0 for those keys not present.