I have array1 and array2 in below data.i wanted to iterate all factoryId of array1 and need to check if same factoryid is present in array2.
if factoryId of array1 is matching with array2 factoryId , than i wanted to prepare new result array with array1 object and status field from array2 into new result array.
Expected output is a combination of array1 and array2 status field.
can anyone help me to do this
const array1 = [
{
"name": "Africa",
"filterChildren": [
[
{
"name": "Agbara - Laundry",
"factoryId": "R_X001"
},
{
"name": "Agbara - Savoury",
"factoryId": "R_X002"
}
]
]
},
{
"name": "X-Ekaterra",
"filterChildren": [
[
{
"name": "Agbara - Tea",
"factoryId": "R_x903"
}
]
]
}
];
const array2 = [
{
"FactoryId": "R_X001",
"Status": "Draft"
},
{
"FactoryId": "R_x903",
"Status": "Submitted"
}
]
Expected Output
Expected Result = [
{
"name": "Africa",
"filterChildren": [
[
{
"name": "Agbara - Laundry",
"factoryId": "R_X001",
"Status": "Draft"
},
{
"name": "Agbara - Savoury",
"factoryId": "R_X002"
}
]
]
},
{
"name": "X-Ekaterra",
"filterChildren": [
[
{
"name": "Agbara - Tea",
"factoryId": "R_x903",
"Status": "Submitted"
}
]
]
}
];
2
Answers
Just run a for loop for the arrays, then use array
find
method to find the element on the second array and update the property ofStatus
.You can achieve this by iterating over array1 and array2, and merging the status from array2 into array1 when the factoryId matches.
Here is a function that can do this functionalitie
and you get the the exact output you want