I have first array –
let parent = [
{
id:1,
value:"ABC",
},
{
id:2,
value:"DEF",
},
{
id:3,
value:"GHI",
},
{
id:4,
value:"JKL",
},
{
id:5,
value:"MNO",
},
{
id:6,
value:"PQR",
},
]
And 2nd Array Object –
let child = [
{
childid:1,
value:"ABC",
},
{
childid:2,
value:"DEF",
},
{
childid:10,
value:"GHI",
},
]
From parent array I want to select all those elements whose id matches with childid from child array.
I tried –
parent.filter(x=>x.id==child.each(y=>y.childid))
But its not working
5
Answers
You can use some() to do it
using Flatmap and filter …
This would work
Wat happens is
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
You can use a
reduce
function along with aforEach
to loop through the child elements and compare against the parent.MDN Reduce