Categories
JavaScript Answers

How to use Lodash to compare jagged arrays?

Spread the love

To use Lodash to compare jagged arrays, we use the isEqual and sort methods.

For instance, we write

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

to call sort on each array to return a sorted version of them.

And then we call isEqual with the sorted array to check if they have the same content.

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 *