2Fast4YouBR
2Fast4YouBR

Reputation: 1042

Javascript json filter deep child

I am trying to filter a json based on the child value: let´s say:

[
    {
        "Date": "2017-03-02T00:00:00",
        "Matches": [
            {
                "Id": 67,
                "NameSeo": "teste-02-33",
                "IsLive": true
            },
            {
                "Id": 63,
                "NameSeo": "teste-ao-vivo-always",
                "IsLive": true
            }
        ]
    },
    {
        "Date": "2017-03-13T00:00:00",
        "Matches": [
            {
                "Id": 64,
                "NameSeo": "san-avai-13-03-17-13-00-caninde",
                "IsLive": false
            },
            {
                "Id": 66,
                "NameSeo": "teste-01-xxx",
                "IsLive": false
            }
        ]
    },
    {
        "Date": "2017-03-23T00:00:00",
        "Matches": [
            {
                "Id": 65,
                "NameSeo": "gre-cor-23-03-17-17-30-pacaembu",
                "IsLive": false
            }
        ]
    }
]

How can I count the "Matches" with "IsLive" = true ? in this example would be 2. Would be 1 date with 2 live matches (but I dont care about the date, just that I have 2 matches live).

The closer I could get is:

x.filter(x => x.Matches.filter(w => w.IsLive)).length

But here it returns 3, that I guess are all the dates.

x.filter(x => x.Matches.IsLive).length

This returns 0

Upvotes: 1

Views: 196

Answers (1)

Jack
Jack

Reputation: 21163

Try:

x.reduce((count, item) => count + item.Matches.filter(w => w.IsLive).length, 0)

Upvotes: 1

Related Questions