I have a similar array in the below format. I need to be able to find the object based on the id and it has to search for all the nested arrays (parent and child). And, the selectedId could be also be from the id of parent array.
Could anyone please advise?
Excerpt from my code
const cars = [{
id: 1,
name: 'toyota',
subs: [{
id: 43,
name: 'supra'
}, {
id: 44,
name: 'prius'
}]
}, {
id: 2,
name: 'Jeep',
subs: [{
id: 30,
name: 'wranger'
}, {
id: 31,
name: 'sahara'
}]
}]
const selectedId = 31;
const result = cars.find((val) => val.subs.id === selectedId)
console.log(result)
My expected output should be
{id: 31, name: 'sahara'}