here I have two array of objects which looks like this,
const arr1 = [
{
_id: "63e5cbadd926a20ade863c44",
productId: "63de474a561e0319a574552b"
},
{
_id: "63e5cbadd926a20ade863c45",
productId: "63de47c7561e0319a5745531"
},
{
_id: "63e5cbadd926a20ade863c46",
productId: "63dea93bdf662740f4ba37fe"
}
]
and other array looks like this,
const arr2 = [
{
_id: "63de474a561e0319a574552b",
categoryOneId: [Object],
productPrice: 439.89
},
{
_id: "63de47c7561e0319a5745531",
categoryOneId: [Object],
productPrice: 56.9
},
{
_id: "63dea93bdf662740f4ba37fe",
categoryOneId: [Object],
productPrice: 56.9
}
]
now I need something like below, where I need all the elements from from array two and need productId field attached to every obj. Please find the expected output below.
[
{
_id: "63de474a561e0319a574552b",
categoryOneId: [Object],
productPrice: 439.89,
orderId: "63e5cbadd926a20ade863c44"
},
{
_id: "63de47c7561e0319a5745531",
categoryOneId: [Object],
productPrice: 56.9,
orderId: "63e5cbadd926a20ade863c45"
},
{
_id: "63dea93bdf662740f4ba37fe",
categoryOneId: [Object],
productPrice: 56.9,
orderId: "63e5cbadd926a20ade863c46"
}
]
I was trying something like this,
for (let i = 0; i < arr2.length; i++) {
const element = arr2[i];
if (productIds.find(e => e.productId === element._id )) {
arr2[i].productId = arr1[i].productId
}
}
can anyone please help me to fix this.
Thanks.