Unordered string array comparison with lodash

Viewed 3681

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.

1 Answers

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>

Related