compare arrays in javascript
there is no native method in js to compare 2 arrays.
solution is to
first sort the array and
then convert to strings and
then compare strings.
solution is to
first sort the array and
then convert to strings and
then compare strings.
var a1 = [1,2,3];
var a2 = [1,2,3];
console.log(a1==a2); // Returns false
newa1 = a1.sort();
newa2 = a2.sort();
console.log(JSON.stringify(newa1)===JSON.stringify(newa2)); // Returns true
Comments
Post a Comment