difference([1, 2, 3], [1, 2, 4]); // [3] -
Filters out all values from an array for which the comparator function does not return true.
Use Array.filter() and Array.find() to find the appropriate values.
const differenceWith = (arr, val, comp) => arr.filter(a => !val.find(b => comp(a, b))); -
differenceWith([1, 1.2, 1.5, 3], [1.9, 3], (a, b) => Math.round(a) == Math.round(b)); // [1, 1.2] +
Filters out all values from an array for which the comparator function does not return true.
Use Array.filter() and Array.findIndex() to find the appropriate values.
const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b)) === -1); +
differenceWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0], (a, b) => Math.round(a) === Math.round(b)); // [1, 1.2]
Returns all the distinct values of an array.
Use ES6 Set and the ...rest operator to discard all duplicated values.
const distinctValuesOfArray = arr => [...new Set(arr)];
distinctValuesOfArray([1, 2, 2, 3, 4, 4, 5]); // [1,2,3,4,5]
Removes elements in an array until the passed function returns true. Returns the remaining elements in the array.
Loop through the array, using Array.slice() to drop the first element of the array until the returned value from the function is true. Returns the remaining elements.
const dropElements = (arr, func) => {