ds97
ds97

Reputation: 107

Filter from array within array in typescript

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

Answers (2)

radulle
radulle

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

V-Mann_Nick
V-Mann_Nick

Reputation: 56

You will need to check if the types array includes "administrative":

places.filter(place => place.types.includes("administrative"))

Upvotes: 0

Related Questions