JavaScript:为什么我的过滤器功能不起作用?

时间:2015-04-23 23:10:42

标签: javascript functional-programming

我正在尝试过滤掉数组中" EventNames.Name中有空白字符串的项目。"

为了将链接保持在最低限度,我将我的过滤器分成了单独的变量。这是我的功能:

function listViewFilter(){

  var NameFiltered = DateFiltered.map(function (property){ // remove blank EventNames.Name
      return property.EventNames.filter(function (subprop){
         return subprop.Name !== "";
      });
  })

  Filtered = NameFiltered;
}

仍然没有返回正确的结果。有人可以帮忙吗?我想知道我是否应该进行链接,而不是嵌套。

{
   "EventNames":[
      {
         "Name":"",
         "EventDate":"APRIL 19, 2015",
         "Ticket":"/losangeles/events/eventdetail/?viewNav=/event-detail&eventId=undefined&oid=undefined"
      }
   ],
   "TMEvents":[

   ],
   "SpecialEvents":[

   ],
   "TMEventImage":[
      {
         "Name":"",
         "EventDate":"APRIL 19, 2015"
      }
   ],
   "Artists":[
      {

      }
   ],
   "EventOffers":[
      {

      }
   ],
   "CrossRoadEvents":[

   ],
   "ImageUrls":[
      {

      }
   ],
   "TicketUrls":[
      {
         "Ticket":"/losangeles/events/eventdetail/?viewNav=/event-detail&eventId=undefined&oid=undefined"
      }
   ],
   "MonthVals":[
      {
         "MonthVal":"APRIL 19 2015"
      }
   ],
   "EventDate":"APRIL 19 2015",
   "VenueName":"House of Blues Las Vegas"
}

2 个答案:

答案 0 :(得分:0)

http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#device-memory

DateFiltered.EventNames = DateFiltered.EventNames.filter(function (property){
     return property.Name !== ""
})

答案 1 :(得分:0)

Array.prototype.reduce方法:

  

reduce()方法对累加器应用函数,并且数组的每个值(从左到右)必须将其减少为单个值。

如果checked元素正在传递我们的规则 - 我们需要它,否则需要检查另一个元素:

function filterByRule(entries, callback) {
  return entries.reduce(function(memo, entry) {
    if(callback(entry)) memo.push(entry);
    return memo;
  }, []);
}

console.log('filteredEntries', filterByRule(entries, function(entry) {
  var result;
  try {
    result = entry.EventNames[0].Name && !/010/.test(entry.EventDate); // test criteria
  } catch(e) {
    console.log('Match returns error:', e);
  }
  return result;
}))