I need to extract listImages that have a status of 'Existing` inside of an array. My problem is that it gets its parent array.
Data
lists = [
{
"listID": "1",
"listImages": [
{
"id": "111",
"status": "Existing"
},
{
"id": "222",
"status": "Non-Existing"
}
]
},
{
"listID": "2",
"listImages": [
{
"id": "333",
"status": "Non-Existing"
}
]
}
]
Current Code
const images = lists.map((list) => ({
...list,
listImages: (list.listImages?? []).filter(
({ status }) => status === "Existing"
),
}));
Expected Output
["111"]
Array.prototype.reduce(), like:lists.reduce((acc, {listImages}) => (listImages.forEach(({id, status}) => status === 'Existing' && acc.push(id)), acc), [])