I searched a while but couldn't find the answer that I want. I am have a very simple question, how to get rid of the empty objects from Map.
const friuts = [{
apple: 'red',
banana: 1
},
{
apple: 'green',
banana: 1
},
{
apple: 'yellow',
banana: 3
}
]
const newObject = friuts.map(e =>
({ ...e.banana === 1 ? {
apple: e.apple
} :
[]
})
)
console.log(newObject)
If you check the console.log it contains an empty object at the end
[
{
"apple": "red"
},
{
"apple": "green"
},
{} <--- empty
]
Also I tried undefined or below code, but just can't get rid of the empty objects.
...e.banana === 1 &&
{
apple: e.apple
}
I understand this can be easily done by using other methods like filter. However, I am learning Map, so I'd like to learn how to get rid of the empty objects from map.
Sorry for if the question has been asked before. I will remove the question if it is duplicated.
maponly, it will make little sense, since the code should be readable. For each task you need to use the correct tool, and the tool here isreduce.