Get an element inside an array based on a condition using find is known to me, but what if array is nested with an object?
[{
_id: 1,
crew: [{
name: "A",
price: "5",
},
{
name: "B",
price: "10",
},
],
},
{
_id: 2,
crew: [{
name: "C",
price: "15",
},
{
name: "D",
price: "20",
},
],
},
];
I want to search by name, let D be an example
Expected result
{
name: "D",
price: "20",
},
thank you
3
Answers
You can try using
Array.prototype.reduce()
:Code Example:
You could try this out
You can do something like this: