Posts

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. 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 (new a1 )=== JSON . stringify (new a2 )); // Returns true