Last active
July 8, 2021 09:32
-
-
Save olecksamdr/ff3fb42776f396d48ca4ebd22f17a614 to your computer and use it in GitHub Desktop.
differenceWith function. Performs two arrays difference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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