0

Is there a way I can determine the intersection between 2 arrays where the indexing is different, like this:

a1 = ["a", "b"]
a2 = ["b", "a"]

/* a1 == a2 should return true */

and of course,

a1 = ["b", "c"]
a2 = ["d", "f"]

/* a1 == a2 should return false */

Or is there any better way to check the similarity between 2 arrays where indexing is different in jQuery/Javascript

Crashtor
  • 1,249
  • 1
  • 13
  • 21
  • Your answer is in the duplicate. Note that jQuery is a framework primarily intended for amending the DOM. As such it's not what you need to use here; plain old JS is. – Rory McCrossan Mar 02 '20 at 16:47

1 Answers1

1

Use sort().toString()

var a1 = ["a", "b"]
var a2 = ["b", "a"]

console.log(a1.sort().toString()===a2.sort().toString());

var a1 = ["b", "c"]
var a2 = ["d", "f"]
console.log(a1.sort().toString()===a2.sort().toString());
Sunil Lama
  • 4,531
  • 1
  • 18
  • 46