Reputation: 1499
I have a dataset that is an array of objects.
It looks something like this:
[
{
"id": 1,
"name": "john",
"address": { "number": 42, "street": "High Street"}
},
{
"id": 2,
"name": "jane",
"address": Null
},
{
"id": 3,
"name": "hugh",
"address": { "number": 64, "street": "Long Street"}
}
]
I am trying to get a list of all streets from the dataset by doing something like this:
const streets = this.dataset.map((d) => d.address.street).sort();
However, due to address sometimes being null, I am getting a cannot read property street of null
error.
Is there a way I can simply ignore nulls when using the map function?
I tried .filter(Boolean)
but that doesn't work as the object itself isn't null.
Upvotes: 0
Views: 988
Reputation: 5972
You can filter by the address
property:
this.dataset.filter(d => d.address).map(d => d.address.street).sort()
Upvotes: 1