1

I am trying to filter out items in the array that have a blank string in "EventNames.Name."

In order to keep the chaining to a minimum, I separated my filter into separate variables. Here is my function:

function listViewFilter(){

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

  Filtered = NameFiltered;
}

It is still not returning the correct result. Can anyone help? I am wondering if I should be chaining, rather than nesting.

{
   "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"
}
  • You're not supposed to nest `filter` calls. What are you trying to do? Please show your example input and expected output in standard JSON format, what you currently posted is unusable (doesn't show the structure) – Bergi Apr 23 '15 at 23:17
  • do you want to remove Name property if it is empty? or the whole element with empty name? – Said Kholov Apr 23 '15 at 23:21
  • Ok, should be more readable now. I want to remove entries that have an empty EventNames.Name property. –  Apr 23 '15 at 23:27
  • @Triode what is single entry? Element of array EventNames or whole Event object? – Krzysztof Safjanowski Apr 23 '15 at 23:34
  • The whole object should be filtered out if EventNames.Name is an empty string. –  Apr 23 '15 at 23:38
  • Are you sure DateFiltered is array? i.e. not object. filter() function won;t work for objects. You will need to use NameFiltered.EventNames.filter(function(){}) – Said Kholov Apr 23 '15 at 23:38
  • You are correct. DateFiltered is an object. I think you have pointed me in the right direction now. –  Apr 23 '15 at 23:43
  • @Triode did you solve your problem, mate? – Said Kholov May 22 '15 at 04:18

2 Answers2

0

http://jsfiddle.net/udmkmLd8/7/

DateFiltered.EventNames = DateFiltered.EventNames.filter(function (property){
     return property.Name !== ""
})
Said Kholov
  • 2,436
  • 1
  • 15
  • 22
  • Just in case you need filter for objects: http://stackoverflow.com/questions/5072136/javascript-filter-for-objects – Said Kholov Apr 23 '15 at 23:46
  • When I console.log DateFiltered.EventNames it returns undefined. Not sure why. –  Apr 24 '15 at 16:36
  • This works though. DateFiltered[0].EventNames. Do I need to use map before filter? –  Apr 24 '15 at 16:43
0

Array.prototype.reduce method:

The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) has to reduce it to a single value.

If checked element is passing our rule - we want it, otherwise get another element to check:

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;
}))
Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47