A concise way to determine whether two arrays are equal

Js cannot directly use == or === to determine whether two arrays are equal or not. It needs to convert the array to a string before judging.

  • Return false if you use either == or ===

Ex. :

console.log([6.7.8] = = [6.7.8]);
console.log([6.7.8] = = = [6.7.8]);
console.log(["xx"."yy"."zz"] = = ["xx"."yy"."zz"]);
console.log(["xx"."yy"."zz"] = = = ["xx"."yy"."zz"]); 
Copy the code
  • Return true if equal, false if not

Ex. :

 console.log([111.222.333].toString()==[111.222.333].toString());
 console.log(["xx"."yy"."zz"].toString()===["xx"."yy"."zz"].toString());
 // Equality returns true
 console.log([111.222.333].toString()==[999.222.333].toString());
 console.log(["xx"."yy"."zz"].toString()===["hh"."yy"."zz"].toString());
 // Unequal returns false
Copy the code
  • When the array elements are the same but the order of the elements is different, sort the two arrays first and then determine whether the two arrays are equal

Ex. :

 console.log([1.2.3].sort.toString()==[3.1.2].sort.toString()); 
 // Sort first and judge later
Copy the code

Purely exchange learning, if you find loopholes, please find out!