Reputation: 107
This is my array:
places = [
{
"long_name": "Sydney",
"types": [
"locality",
"political"
]
},
{
"long_name": "Perth",
"types": [
"administrative",
"political"
]
}
]
Now I want to filter this array with the types of administrative so that I get the result as Perth.
I am doing this which completely seems wrong.
var city = places.filter(x => x.types === "administrative");
Upvotes: 0
Views: 66
Reputation: 1535
const places = [
{
"long_name": "Sydney",
"types": [
"locality",
"political"
]
},
{
"long_name": "Perth",
"types": [
"administrative",
"political"
]
}
]
const filteredByTypes = places.filter(e => e.types.includes('administrative'))
console.info(filteredByTypes)
You need to use includes
array method.
MDN
Upvotes: 1
Reputation: 56
You will need to check if the types
array includes "administrative"
:
places.filter(place => place.types.includes("administrative"))
Upvotes: 0