or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

lodash.difference

The 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.

Package Information

  • Package Name: lodash.difference
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.difference

Core Imports

const difference = require('lodash.difference');

For ES modules (if supported by your environment):

import difference from 'lodash.difference';

Basic Usage

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]

Capabilities

Array Difference

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:

  • Equality Comparison: Uses SameValueZero equality (similar to === but treats NaN as equal to NaN)
  • Input Validation: Returns empty array if first argument is not array-like
  • Array-like Support: Accepts array-like objects (objects with numeric indices and length property)
  • Variable Arguments: Accepts any number of comparison arrays as additional arguments
  • Flattening: Comparison arrays are flattened before processing
  • Order Preservation: Result maintains the order of elements from the original array

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]