Javascript json过滤深儿

时间:2017-03-03 21:49:22

标签: javascript arrays json filter

我试图根据子值过滤json:让我们说:

[
    {
        "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
            }
        ]
    }
]

如何用“IsLive”= true计算“匹配”?在这个例子中将是2.将是2个实时比赛的1个约会(但我不关心日期,只是我有2场比赛直播)。

我能越接近:

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

但是这里它返回3,我猜是所有日期。

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

返回0

1 个答案:

答案 0 :(得分:1)

尝试:

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