When working with a JSON data set, I often face the challenge of filtering based on specific child values. Take the following example:
[
{
"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
}
]
}
]
My goal is to determine how many "Matches" have their "IsLive" value set to true. In this example, the answer should be 2. Essentially, I want to ignore the dates and focus solely on counting live matches.
I've made some attempts using JavaScript filter functions:
x.filter(x => x.Matches.filter(w => w.IsLive)).length
However, this approach returns 3, which includes all the dates as well.
x.filter(x => x.Matches.IsLive).length
Unfortunately, this last method yielded a result of 0.