Last Updated : 15 Jul, 2025
Lodash _.difference() function is used to remove a single element or the array of elements from the original array. This function works pretty much the same as the core function of JavaScript i.e. filter.
Syntax:_.difference(array, [values]);Parameters:
Return Value:Note: We can use a single value or an array of values. But if only a single Integer is given then it will not affect the original array.
Example 1: In this example, we are using the _.difference() method to find the difference of the array.
JavaScript
// Requiring the lodash library
let _ = require("lodash");
// Original array
let array = ["a", 2, 3];
// Values to be removed from
// the original array
let values = [2, 3]
let newArray = _.difference(array, values);
console.log("Before: ", array);
// Printing array
console.log("After: ", newArray);
Output:
Example 2: In this example, we are using the _.difference() method to find the difference of the array. When an empty array is given, there will be no change in the original array.
JavaScript
// Requiring the lodash library
let _ = require("lodash");
// Original array
let array = ["a", 2, 3];
// Values to be removed from
// the original array
let values = []
let newArray = _.difference(array, values);
console.log("Before: ", array);
// Printing array
console.log("After: ", newArray);
Output:
Note: This function returns the original array if the value array is single value, empty array or object of arrays.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4