0

I have an array of object like this

events = [ {
    'summary': 'sample test events1',
    'location': 'coimbatore',
    'start': {
        'date': '2018-08-28',
        'timeZone': 'America/Los_Angeles'
    },
    'end': {
        'date': '2018-08-28',
        'timeZone': 'America/Los_Angeles'
    }
    },
    {
      'summary': 'sample test events2',
      'location': 'coimbatore',
      'start': {
          'date': '2018-08-29',
          'timeZone': 'America/Los_Angeles'
      },
      'end': {
          'date': '2018-08-29',
          'timeZone': 'America/Los_Angeles'
      }
    },
    {
    'summary': 'sample test events4',
    'location': 'coimbatore',
    'start': {
        'date': '2018-08-27',
        'timeZone': 'America/Los_Angeles'
    },
    'end': {
        'date': '2018-08-27',
        'timeZone': 'America/Los_Angeles'
    }
    },
    {
      'summary': 'sample test events5',
      'location': 'coimbatore',
      'start': {
          'date': '2018-08-26',
          'timeZone': 'America/Los_Angeles'
      },
      'end': {
          'date': '2018-08-26',
          'timeZone': 'America/Los_Angeles'
      }
      }];

I have another array of object to filter

toFilterEvents = [
{startDate: "2018-08-28", summary: "sample test events1"},
{startDate: "2018-08-29", summary: "sample test events2"},
]

I want the result to be like,

events = [ 

    {
    'summary': 'sample test events4',
    'location': 'coimbatore',
    'start': {
        'date': '2018-08-27',
        'timeZone': 'America/Los_Angeles'
    },
    'end': {
        'date': '2018-08-27',
        'timeZone': 'America/Los_Angeles'
    }
    },
    {
      'summary': 'sample test events5',
      'location': 'coimbatore',
      'start': {
          'date': '2018-08-26',
          'timeZone': 'America/Los_Angeles'
      },
      'end': {
          'date': '2018-08-26',
          'timeZone': 'America/Los_Angeles'
      }
      }];

What I have tried,

filterExistingEvents(toFilterEvents);

filterExistingEvents(filtered_events) {
    const hello = this.events.filter((r, i) => {
      return filtered_events.some(f => r.summary !== f.summary)
    });
    console.log('events after filter', hello, this.events);  
}

As you can see i am using filter and some to get the desired output like as shown above but it doesnt work. I found similar questions like this but this is to return the events without toFilterEvents

kartik
  • 294
  • 3
  • 15

2 Answers2

2

Array.prototype.some returns true if atleast one of the array element matches the condition Array.prototype.every returns true if all of the array elements matches the condition

filterExistingEvents(filtered_events) {
    const hello = this.events.filter((r, i) => {
      return filtered_events.every(f => r.summary !== f.summary)
    });
    console.log('events after filter', hello, this.events);  
}
AvcS
  • 2,263
  • 11
  • 18
0

It looks like you would like to filter on multiple fields so it may be better to create the filter function out of higher order functions, a detailed example can be found here.

I think you were actually looking for the inversion of some instead of every.

const events = [{
  'summary': 'sample test events1',
  'location': 'coimbatore',
  'start': {
    'date': '2018-08-28',
    'timeZone': 'America/Los_Angeles'
  },
  'end': {
    'date': '2018-08-28',
    'timeZone': 'America/Los_Angeles'
  }
},
{
  'summary': 'sample test events2',
  'location': 'coimbatore',
  'start': {
    'date': '2018-08-29',
    'timeZone': 'America/Los_Angeles'
  },
  'end': {
    'date': '2018-08-29',
    'timeZone': 'America/Los_Angeles'
  }
},
{
  'summary': 'sample test events4',
  'location': 'coimbatore',
  'start': {
    'date': '2018-08-27',
    'timeZone': 'America/Los_Angeles'
  },
  'end': {
    'date': '2018-08-27',
    'timeZone': 'America/Los_Angeles'
  }
},
{
  'summary': 'sample test events5',
  'location': 'coimbatore',
  'start': {
    'date': '2018-08-26',
    'timeZone': 'America/Los_Angeles'
  },
  'end': {
    'date': '2018-08-26',
    'timeZone': 'America/Los_Angeles'
  }
}];

const filterFn = getter => comparer => o =>
  comparer(getter(o))
;
const isNotIn = hayStack => needle =>
  !hayStack.some(x=>x===needle)
;
const summary = o => o.summary;
const startDate = o => o.start.date;
const toFilterEvents = [
  { startDate: "2018-08-28", summary: "sample test events1" },
  { startDate: "2018-08-29", summary: "sample test events2" },
];
//filter out any events that have summary or startDate in toFilterEvents
console.log(
  events.filter(
    filterFn(summary)(isNotIn(toFilterEvents.map(x=>x.summary)))
  ).filter(
    filterFn(startDate)(isNotIn(toFilterEvents.map(x=>x.startDate)))
  ).map(x=>x.summary)
);
//filter out any events that have summary and startDate in toFilterEvents
console.log(
  events.filter(
    (
      (summaries,startDates)=>item=>
        filterFn(summary)(isNotIn(summaries))(item) ||
        filterFn(startDate)(isNotIn(startDates))(item)
    )(toFilterEvents.map(x=>x.summary),toFilterEvents.map(x=>x.startDate))
  ).map(x=>x.summary)
);
HMR
  • 37,593
  • 24
  • 91
  • 160