Simplest way to compare two array in JS

In case of string we can simply use == or === to see if they are same but we can’t use those to see in two arrays are similar or in other words they have same elements.
So this wont work.

const array1 = [1, 2, 3, 4, 5]
const array2 = [1, 2, 3, 4, 5]…


This content originally appeared on DEV Community and was authored by Shuvo

In case of string we can simply use == or === to see if they are same but we can't use those to see in two arrays are similar or in other words they have same elements.
So this wont work.

const array1 = [1, 2, 3, 4, 5]
const array2 = [1, 2, 3, 4, 5]
console.log(array1 == array2) //false

But what if we convert our array to string? Then you can use the comparison operator. This makes the task very easy. We can sort an array using toString method eg. array1.toString() or we can use this hack

console.log([1, 2, 3, 4, 5] + "")
//logs 1,2,3,4,5
console.log(typeof ([1, 2, 3, 4, 5] + ""))
//logs string

So basically if we try to concatenate string(empty string in this case) to an array the array will be converted to a string.
so now we can simply use the arrays as strings and compare them

const array1 = [1, 2, 3, 4, 5]
const array2 = [1, 2, 3, 4, 5]
console.log(array1 + "" == array2 + "") //true

Also if you want it to work with arrays where the elements are not in order you can first sort them. Let's create a utility function for that

function compareArr(arr1, arr2){
    arr1.sort()
    arr2.sort()
    return arr1 + "" == arr2 + ""
}
const array1 = [1, 2, 3, 4, 5]
const array2 = [1, 5, 2, 4, 3]
console.log(compareArr(array1, array2)) // returns true


This content originally appeared on DEV Community and was authored by Shuvo


Print Share Comment Cite Upload Translate Updates
APA

Shuvo | Sciencx (2021-10-18T04:12:10+00:00) Simplest way to compare two array in JS. Retrieved from https://www.scien.cx/2021/10/18/simplest-way-to-compare-two-array-in-js/

MLA
" » Simplest way to compare two array in JS." Shuvo | Sciencx - Monday October 18, 2021, https://www.scien.cx/2021/10/18/simplest-way-to-compare-two-array-in-js/
HARVARD
Shuvo | Sciencx Monday October 18, 2021 » Simplest way to compare two array in JS., viewed ,<https://www.scien.cx/2021/10/18/simplest-way-to-compare-two-array-in-js/>
VANCOUVER
Shuvo | Sciencx - » Simplest way to compare two array in JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/18/simplest-way-to-compare-two-array-in-js/
CHICAGO
" » Simplest way to compare two array in JS." Shuvo | Sciencx - Accessed . https://www.scien.cx/2021/10/18/simplest-way-to-compare-two-array-in-js/
IEEE
" » Simplest way to compare two array in JS." Shuvo | Sciencx [Online]. Available: https://www.scien.cx/2021/10/18/simplest-way-to-compare-two-array-in-js/. [Accessed: ]
rf:citation
» Simplest way to compare two array in JS | Shuvo | Sciencx | https://www.scien.cx/2021/10/18/simplest-way-to-compare-two-array-in-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.