I have two arrays.
The first Array:
let array1 = [
{label:'john', value:1},
{label:'susan', value:2},
{label:'ann', value:3}
]
The second Array:
let array2 = [
{id:1, name:'john', age:12},
{id:3, name:'ann', age:24}
]
What am looking forward is to get all the array1 objects which value matches with id in array2
So basically the solution should have:
{label:'john', value:1}
{label:'ann', value:3}
So I have tried:
let matches = [];
array1.forEach((item1)=>{
array2.forEach((item2)=>{
if(item2.id === item1.value){
matches.push() //stuck here
}
})
})
Am stuck on how to push the items to a new array. Also looking at my code it looks abit messy. How do i refactor the code to work .
{id:1, name:'hubert', age:99}in the second array still be considered a “match” for{label:'john', value:1}from the first?