2017-02-26 59 views
0

JS:得到通知長度取決於ReadFlag

function getJSON(){ 
       $.ajax({ 
        url: "getgroupednotification.json", 
        type: "GET", 
        crossDomain: true, 
        success:function(res){ 

         $.each(res,function(index, value){ 
          //console.log(res); 
          //console.log(value); 
          $.each(value.Notifications, function(index_, value_){ 
           if(value.Source == 'CIRIS'){ 
            var i = value.Notifications.length; 
             if(value_.ReadFlag != 1){ 

            } 
           } 
          }); 
         }); 
       }); 

我想如果ReadFlag == 0,即時通訊使用來源== C光圈在這個例子中得到notification.length。

這是鏈接到我的JSON https://api.myjson.com/bins/navph

+0

能否請你澄清你想要做什麼?你想**的長度**任何**或**只有一個**項目有_ReadFlag_ 0? – caisah

+0

我想要的長度,當任何項目有ReadFlag 0 – poojay

回答

0

你可以使用這樣的事情:

function getJSON() { 
    $.ajax({ 
     url: "https://api.myjson.com/bins/navph", 
     type: "GET", 
     crossDomain: true, 
     success: function(res) { 
      var lens = res.filter(function(item) { 
       // filter by source 
       return item.Source === 'CIRIS'; 
      }).map(function(item) { 
       // get only Notifications 
       return item.Notifications; 
      }).map(function(notification){ 
       // get lengths of notifications which have at least 1 RedFlag not 0 
       return (notification.filter(function(item){ 
        return item.RedFlag !== 0; 
       }).length) 
      }) 
      console.log(lens[0]); 
     } 
    }) 
} 

getJSON(); 
+0

我做了這個。無論readflag是0還是1,它仍然會返回長度的總數 – poojay