7

I'm trying to compare two unordered arrays of strings with lodash. I have tried using the isMatch function but it doesn't seem do what I want. Here's what I have tried:

var arr1 = ['foo', 'bar']
var arr2 = ['bar', 'foo']
_.isEqual(arr1,arr2) //should return true, but instead it returns false

Thanks.

Daedalus
  • 7,586
  • 5
  • 36
  • 61
Dave Kalu
  • 1,520
  • 3
  • 19
  • 38

1 Answers1

3

You need to sort() the arrays to maintain the sequence for comparing it with _.isEqual()

var arr1 = ['foo', 'bar']
var arr2 = ['bar', 'foo']
console.log(_.isEqual(arr1.sort(),arr2.sort()));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62