Categories
JavaScript Answers

How to use lodash to compare jagged arrays with JavaScript?

Spread the love

Sometimes, we want to use lodash to compare jagged arrays with JavaScript.

In this article, we’ll look at how to use lodash to compare jagged arrays with JavaScript.

How to use lodash to compare jagged arrays with JavaScript?

To use lodash to compare jagged arrays with JavaScript, we can sort the arrays with sort and then use isEqual to compare their contents.

For instance, we write

const array1 = [
  ["a", "b"],
  ["b", "c"],
];
const array2 = [
  ["b", "c"],
  ["a", "b"],
];
_.isEqual(array1.sort(), array2.sort());

to call sort to sort both arrays.

And then we call Lodash’s isEqual function to compare the contents of the sorted arrays.

Conclusion

To use lodash to compare jagged arrays with JavaScript, we can sort the arrays with sort and then use isEqual to compare their contents.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *