I have two large arrays, one of items, the other of names who have the items. I need to push all the names who have the matching items into array2 items.
I can do this via a nest forEach loop:
array1 = [
{item: "1", name: "Joe" },
{item: "2", name: "Sam" },
{item: "1", name: "Alice"},
{item: "3", name: "Peter"},
{item: "1", name: "Jack"},
]
array2 = [
{ item: "1", names: []},
{ item: "2", names: []},
{ item: "3", names: []},
]
array2.forEach(x => {
array1.forEach(y => {
if( x.item === y.item ){
x.names.push(y.name)
}
})
})
console.log(array2)
But I have a sense this is bad practice and not to mention resource heavy on large arrays.
What is the moden way to do this?