I have two array of objects
const oldArr = [
{
assetDetail_ID: 1,
asset_Condition: "",
asset_Condition_ID: 0,
supplier_ID: 5,
},
{
assetDetail_ID: 2,
asset_Condition: "Good",
asset_Condition_ID: 3,
supplier_ID: 10,
},
];
const newArr = [
{
assetDetail_ID: 1,
supplier_ID: 40,
},
{
assetDetail_ID: 2,
supplier_ID: 30,
},
];
I am trying find common values by checking with object key and if they are the same, get key and value pair into a new array so my final result will be
expectedResult = [
{
assetDetail_ID: 1,
supplier_ID: 5,
},
{
assetDetail_ID: 2,
supplier_ID: 10,
},
];
I have tried this but I am only getting values as [1, 5, 2, 10]and not objects , what am I doing wrong here ?
const oldArr = [{
assetDetail_ID: 1,
asset_Condition: "",
asset_Condition_ID: 0,
supplier_ID: 5,
},
{
assetDetail_ID: 2,
asset_Condition: "Good",
asset_Condition_ID: 3,
supplier_ID: 10,
},
];
const newArr = [{
assetDetail_ID: 1,
supplier_ID: 40,
},
{
assetDetail_ID: 2,
supplier_ID: 30,
},
];
const arr = []
oldArr.forEach((one, x) => {
for (let i in one) {
for (let j in newArr[x])
if (i === j) {
arr.push(one[i]); // If I change to arr.push(one);, it adds the whole object
}
}
});
console.log(arr)