Samman Thapa Samman Thapa - 4 months ago 8
Node.js Question

Should.js: check if two arrays contain same strings

I have two arrays:

var a = ['a', 'as', 'sa'];
var b = ['sa', 'a', 'as'];


Is there anything special in shouldJS to test if these two arrays have same items? Anything Like

should(a).be.xyz(b)


that can test them? Here, xyz is what I am looking for.

Answer

A naive, but possibly sufficient solution would be to sort the arrays before comparing them:

should(a.sort()).be.equal(b.sort())

Note that sort() works in-place, mutating the original arrays.