D
D
Danoneko2020-04-19 16:28:04
JavaScript
Danoneko, 2020-04-19 16:28:04

How to quickly compare 2 numeric arrays?

Is it possible to somehow quickly compare numeric arrays:

arr1 = [1,2,3];
arr2 = [1,2,3];
arr3 = [3,2,1];

Type design:
if(arr1 == arr2 || arr1 == arr3){
 alert('correct');
}
else{alert('not right');}


For some reason it doesn't work. What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Shvedov, 2020-04-19
@Danoneko

the == operator does not compare arrays element by element, it compares references, and they are obviously different. The most common way to compare such small arrays is to cast them to a string using JSON.stringify() and compare the strings

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question