Skip to content

Instantly share code, notes, and snippets.

@olecksamdr
Last active July 8, 2021 09:32
Show Gist options
  • Save olecksamdr/ff3fb42776f396d48ca4ebd22f17a614 to your computer and use it in GitHub Desktop.
Save olecksamdr/ff3fb42776f396d48ca4ebd22f17a614 to your computer and use it in GitHub Desktop.
differenceWith function. Performs two arrays difference
/**
* Filter out element from excludeFrom array if it equals
* to some of the elements in valuesToExclude array
* Inspired by lodash differenceWith
*
* @param {Array} excludeFrom array to exclude from
* @param {Array} valuesToExclude values which we need to exclude from first array
* @param {Function} comparator returns true if two elements are equal
* @returns {Array} Returns the new array.
* @example
*
* differenceWith(
* [{ 'x': 1}, { 'x': 2 }],
* [{ 'x': 1 }],
* (a, b) => a.x === b.x
* );
*
* // => [{ 'x': 2 }]
*/
export const differenceWith = (excludeFrom, valuesToExclude, comparator) =>
excludeFrom.filter(
item => !valuesToExclude.some(exclude => comparator(item, exclude))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment