Javascript and algorithm gurus...
Say I have 2 lists with a matching field and I want to use one list to get all the objects in the other list that match.
List1[
{
number: 1,
other stuff we dont care about
},
{
number: 2,
stuff
},
{
number: 3,
stuff
}
]
List2[
{
number: 1
},
{
number :2
}
]
so I want to feed in both lists and return a List3 that has the first 2 objects of List1 (since the number field matches the number fields of List2)
I can do it with a for loop like this, but theres gotta be a way to do it with javascript methods.
Code:
let matchedThings: Thing[] = [];
let matchedThing: Thing;
for (let i = 0; i < list2.length; i++) {
matchedThing= list1.find(list1thing=> list1thing.number===list2[i].number);
matchedThings.push(matchedThing);
}
return matchedThings;
}
Any idea how to do this with javascript methods? I tried combing filter, find and forEach but couldnt get it.