I know I can do it using loops, but I'm trying to find an elegant way of doing this:
I have two jagged arrays (array of arrays):
var array1 = [['a', 'b'], ['b', 'c']];
var array2 = [['b', 'c'], ['a', 'b']];
I want to use lodash
to confirm that the above two jagged arrays are the same. By 'the same' I mean that there is no item in array1
that is not contained in array2
. Notice that the items in jagged array are actually arrays. So I want to compare between inner arrays.
In terms of checking equality between these items:
['a', 'b'] == ['b', 'a']
or
['a', 'b'] == ['a', 'b']
Both work since the letters will always be in order.
UPDATE: Original question was talking about to "arrays" (instead of jagged arrays) and for years many people discussed (and added answers) about comparing simple one-dimensional arrays (without noticing that the examples provided in the question were not actually similar to the simple one-dimensional arrays they were expecting).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…