2017-03-03 82 views
2

我试图筛选基于孩子值一个json:let's说:的Javascript 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” =真?在这个例子中会是2.会有2个现场比赛的1日期(但我不在乎日期,只是我有2场比赛现场)。

越接近我能得到的是:

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

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

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

这将返回0

+0

'x.filter(X => x.Matches.filter(W => w.IsLive))length'不因为'x.Matches.filter(w => w.IsLive)'总是返回一个数组,如果你在外部'filter'中返回它,它总会被认为是真的。这就是为什么所有3个对象都计入结果中的原因。 – Xufox

+0

这是一个我忘了放的问题,现在我明白它为什么会返回3 ..谢谢。 – 2Fast4YouBR

回答

1

尝试:

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