I have two array of objects as following
const arr1 = [
{
location_ID: 1,
employee: "name",
status: "available",
},
{
location_ID: 2,
employee: "name",
status: "available",
},
];
const arr2 = [
{
assetLocation_ID: 1,
location_Name: "Yangon",
},
{
assetLocation_ID: 2,
location_Name: "Mandalay",
},
{
assetLocation_ID: 3,
location_Name: "Magway",
},
];
I am trying to find location name and insert a new field in arr1 by location_ID so the final result would be like
const arr1 = [
{
location_ID: 1,
employee: "name",
location_Name: "Yangon",
status: "available",
},
{
location_ID: 2,
employee: "name",
location_Name: "Mandalay",
status: "available",
},
];
I've tried for loop in arr1 and arr2.find for arr2 but objects of arr1 is always having location of ID 1, what am I doing wrong here ? Here is my code so far
for (let i in arr1) {
arr1[i].asset_Location = arr2.find((one) => {
// one is always showing the first object of arr2
return (arr1[i].location_ID = one.assetLocation_ID);
}).location_Name;
}
==herearr1[i].location_ID = one.assetLocation_ID