Creates arrays of unique values from the first array that are not included in other provided arrays using SameValueZero equality comparisons
npx @tessl/cli install tessl/npm-lodash--difference@3.2.0The modern build of lodash's _.difference as a modular npm package. Creates arrays of unique values from the first array that are not included in other provided arrays using SameValueZero equality comparisons.
npm install lodash.differenceconst difference = require('lodash.difference');For ES modules (if supported by your environment):
import difference from 'lodash.difference';const difference = require('lodash.difference');
// Basic array difference
const result1 = difference([1, 2, 3], [4, 2]);
// => [1, 3]
// Multiple comparison arrays
const result2 = difference([1, 2, 3, 4, 5], [2, 4], [1]);
// => [3, 5]
// With strings
const result3 = difference(['a', 'b', 'c'], ['b', 'd']);
// => ['a', 'c']
// With mixed types
const result4 = difference([1, '2', 3, '4'], [1, '4']);
// => ['2', 3]Creates an array of unique values from the first array that are not included in the other provided arrays.
/**
* Creates an array of unique values from the first array that are not included in the other provided arrays.
* Uses SameValueZero equality comparisons for determining inclusion.
*
* @param {Array} array - The array to inspect
* @param {...Array} [values] - The arrays of values to exclude
* @returns {Array} Returns the new array of filtered values
*/
function difference(array, ...values);Behavior Details:
=== but treats NaN as equal to NaN)length property)Usage Examples:
const difference = require('lodash.difference');
// Empty array handling
difference(null, [1, 2]); // => []
difference(undefined, [1, 2]); // => []
difference('not-array', [1, 2]); // => []
// Array-like objects
difference({ 0: 'a', 1: 'b', 2: 'c', length: 3 }, ['b']);
// => ['a', 'c']
// Nested comparison arrays (flattened)
difference([1, 2, 3, 4], [[2, 3]], [4]);
// => [1] (comparison arrays are flattened: [2, 3, 4])
// NaN handling (SameValueZero equality)
difference([1, NaN, 3], [NaN]);
// => [1, 3]
// No comparison arrays
difference([1, 2, 3]);
// => [1, 2, 3]