Skip to main content
Question Protected by Mark Rotteveel
make title into a question
Link
Mulan
  • 136.1k
  • 35
  • 240
  • 276

Comparing two How to compare arrays in JavascriptJavaScript?

"stringify" is the standard in javascript, not "encode"
Source Link
nategood
  • 12k
  • 4
  • 38
  • 45

I'd like to compare two arrays... ideally, efficiently. Nothing fancy, just true if they are identical, and false if not. Not surprisingly, the comparison operator doesn't seem to work.

var a1 = [1,2,3]; var a2 = [1,2,3]; console.log(a1==a2); // Returns false console.log(JSON.encodestringify(a1)==JSON.encodestringify(a2)); // Returns true 

JSON encoding each array does, but is there a faster or "better" way to simply compare arrays without having to iterate through each value?

I'd like to compare two arrays... ideally, efficiently. Nothing fancy, just true if they are identical, and false if not. Not surprisingly, the comparison operator doesn't seem to work.

var a1 = [1,2,3]; var a2 = [1,2,3]; console.log(a1==a2); // Returns false console.log(JSON.encode(a1)==JSON.encode(a2)); // Returns true 

JSON encoding each array does, but is there a faster or "better" way to simply compare arrays without having to iterate through each value?

I'd like to compare two arrays... ideally, efficiently. Nothing fancy, just true if they are identical, and false if not. Not surprisingly, the comparison operator doesn't seem to work.

var a1 = [1,2,3]; var a2 = [1,2,3]; console.log(a1==a2); // Returns false console.log(JSON.stringify(a1)==JSON.stringify(a2)); // Returns true 

JSON encoding each array does, but is there a faster or "better" way to simply compare arrays without having to iterate through each value?

Source Link
Julian H. Lam
  • 26.3k
  • 13
  • 51
  • 74

Comparing two arrays in Javascript

I'd like to compare two arrays... ideally, efficiently. Nothing fancy, just true if they are identical, and false if not. Not surprisingly, the comparison operator doesn't seem to work.

var a1 = [1,2,3]; var a2 = [1,2,3]; console.log(a1==a2); // Returns false console.log(JSON.encode(a1)==JSON.encode(a2)); // Returns true 

JSON encoding each array does, but is there a faster or "better" way to simply compare arrays without having to iterate through each value?