In my code, there is an array called listSelected
that gets updated after each selection in another grid.
const listSelected =
[{
"_id": "10",
"age": 35,
"name": "Paige Zamora",
"gender": "female",
"company": "AUTOMON",
"**reference_id": "12**"
},
{
"_id": "11",
"age": 40,
"name": "Jennifer Carr",
"gender": "female",
"company": "SYNKGEN",
"**reference_id": "11**"
}];
The goal is to cross-reference the values of reference_id
with another table named data
. If a matching value exists, we extract the corresponding id
into a new array. This operation should be done even if the same reference_id
appears multiple times.
const data = [{
"_id": "**10**",
"age": 35,
"name": "Paige Zamora",
"gender": "female",
"company": "AUTOMON",
"**reference_id": "12**"
},
...
];
The final result will be displayed as:
const newGrid = [11,12]
I am considering using nested functions utilizing forEach
loops to achieve this, like so:
const newGrid = [];
listSelected.forEach((elt) => {
data.forEach((item) => {
if (item.reference_id === elt.reference_id) {
newGrid.push(item.id);
newGrid = Array.from(new Set(newGrid));
}
});
});
Are there any alternative approaches to simplify this function and avoid using nested forEach
loops?