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

4
  • use every instead of some Commented May 31, 2018 at 15:07
  • I tried. It doesnt work. Commented May 31, 2018 at 15:08
  • Can you post the error message you are getting? Commented May 31, 2018 at 15:09
  • filtered_events.every(f => r.summary !== f.summary && r.start.date !== f.startDate) Commented May 31, 2018 at 15:10

2 Answers 2

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);  
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thats what he wants, check the output he posted
Right, nevermind... Still some words of explanation would be good
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)
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.