The difference() function in Lodash

The difference() function in Lodash

The difference() function in Lodash

In this post, we will give you information about The difference() function in Lodash – onlinecode. Here we will give you detail about The difference() function in Lodash – onlinecode And how to use it also give you a demo for it if it is necessary.

The difference() function in Lodash takes two arrays, and returns an array containing all the values in the first array that are not in the second array.

const _ = require('lodash');
const array = [1, 2, 3, 4, 5];
const values = [1, 2, 3];
_.difference(array, values); // returns [4, 5]

The difference() function also supports passing in multiple arrays.
If you pass in 3 or more arrays, Lodash returns an array containing all values that are in the first array, but not in any of the subsequent arrays.

_.difference([1, 2, 3, 4, 5], [1, 3], [2, 4]); // [5]

differenceBy()

The differenceBy() function accepts a third argument, a callback function that transforms each element before calculating the difference.
For example, below is how you can calculate the difference between two sets of numbers by their Math.floor() value.

const _ = require('lodash');
const array = [1.1, 1.2, 2.1, 2.2, 3.3, 4, 5];
const values = [1.4, 2.3, 3.2];
_.differenceBy(array, values, Math.floor); // returns [4, 5]

You can also use this function with arrays of objects.

const _ = require('lodash');
const array = [{property: 1, property: 2}];
const values = [{property: 1}];
_.differenceBy(array, values, 'property'); // returns [{property: 2}]

You can even use it to convert values to strings.
This is convenient for calculating the difference between arrays of Mongoose ObjectIds.

const mongoose = require('mongoose');
const _ = require('lodash');

const oids1 = [
  mongoose.Types.ObjectId('123456789012'),
  mongoose.Types.ObjectId('123456789013'), 
  mongoose.Types.ObjectId('123456789014')
];
const oids2 = [
  mongoose.Types.ObjectId('123456789013'),
  mongoose.Types.ObjectId('123456789014')
];

// Doesn't work! Returns all elements of 'oids1', because ObjectIds
// are objects.
_.difference(oids1, oids2);

// Works! Diffs ObjectIds by their string values.
_.differenceBy(oids1, oids2, id => id.toString()); // [mongoose.Types.ObjectId('123456789012')]

differenceWith()

The differenceWith() function takes a comparator function as a third parameter.
The comparator should accept two values, and return true if the values should be considered equal.
The differenceWith() function is often used to calculate the difference between arrays of objects using Lodash’s deep equality check to compare JavaScript objects.

const _ = require('lodash');
const array = [
  { x: 1, y: 2 },
  { x: 2, y: 1 },
];
const values = [{ x: 2, y: 1 }];
_.differenceWith(array, values, _.isEqual) // returns [{x: 1, y: 2}]

 

Lodash is a JavaScript library which provides utility functions for common programming tasks using the functional programming paradigm. Lodash is a JavaScript library that helps programmers write more concise and maintainable JavaScript. It can be broken down into several main areas: Utilities: for simplifying common programming tasks such as determining type as well as simplifying math operations.

Why Lodash?

Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.Lodash’s modular methods are great for:

  • Iterating arrays, objects, & strings
  • Manipulating & testing values
  • Creating composite functions

Hope this code and post will helped you for implement The difference() function in Lodash – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US