Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
Can anyone help me how I can able to compare tow arrays in JavaScript? I tried with a comparison operator but it is not working.

1 Answer

0 votes
by (26.7k points)

Basically, in able to compare two arrays, first, we need to sort the array which will give the accurate result:

const array2Sorted = array2.slice().sort();

array1.length === array2.length && array1.slice().sort().every(function(value, index) {

    return value === array2Sorted[index];

});

This will first sort the array and then it gives the output as true.

I hope this will help.

Want to become a Java Expert? Join Java Training now!!

Want to know more about Java? Watch this video on Java Course | Java Tutorial for Beginners | Java Training:

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 14, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer

Browse Categories

...