Reputation: 411
I am having array person
of say 3 objects and form that i want to return objects named description
. Now m using map to iterate over person
, but what happens here is, if person is not having description
then it returns undefined
. I want at last to get array with only description
objects(no undefined).
const person = [
{abc: 'abc',description:{}},
{qwe:'qwe', def:'def'},
{abcd: 'abcd',description:{}}
]
console.log(person.map(indivi => indivi.description))
Upvotes: 0
Views: 29
Reputation: 84922
You could filter them out with .filter:
const person = [
{abc: 'abc',description:{}},
{qwe:'qwe', def:'def'},
{abcd: 'abcd',description:{}}
]
const descriptions = person
.filter(indivi => indivi.description)
.map(indivi => indivi.description);
Upvotes: 1
Reputation: 14541
You could apply a filter
to remove objects not having a description
property, before calling map
on objects.
const person = [
{abc: 'abc',description:{}},
{qwe:'qwe', def:'def'},
{abcd: 'abcd',description:{}}
]
console.log(person.filter(t => t.description).map(indivi => indivi.description))
Upvotes: 0