Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unordered string array comparison with lodash [duplicate]

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.

like image 225
Dave Kalu Avatar asked Oct 16 '22 13:10

Dave Kalu


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>
like image 96
Ankit Agarwal Avatar answered Nov 15 '22 06:11

Ankit Agarwal