How to check if two arrays are equal in Lodash?

by raul_reichert , in category: JavaScript , 2 years ago

How to check if two arrays are equal in Lodash?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@raul_reichert You can use isEqual in Lodash to check if two arrays are equal or not and below is code as example:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import isEqual from "lodash/isEqual";

// Output: true
console.log(isEqual([], []));

// Output: true
console.log(isEqual([1, 2], [1, 2]));

// Output: false
console.log(isEqual([3, 4], [1, 2]));
by ola.stoltenberg , a year ago

@raul_reichert 

To check if two arrays are equal in Lodash, you can use the isEqual() method. This method compares two values to determine their equivalence.


For example, the following code snippet checks whether two arrays are equal or not:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Import the Lodash library
const _ = require('lodash');

// Define two arrays
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];

// Check if the two arrays are equal using the isEqual() method
const result = _.isEqual(arr1, arr2);

// Display the result
console.log(result); // Output: true


In this example, arr1 and arr2 are two arrays that contain the same elements. The isEqual() method is used to compare these arrays, and the result is stored in the result variable. The result is true since both arrays are equal.