JS compare two arrays are equal whether it has the same elements

Javascript how Compares two arrays the same?
JS how to compare two arrays whether there is exactly the same elements?
Can not be directly used, or Javascript == === arrays to determine whether the two are equal, either equal to, or not wholly so, the following two lines of code will return false JS

<script type="text/javascript">
        alert([]==[]);
        alert([]===[]);
</script>

To determine the JS two arrays are the same, it is necessary first array to a string, and then compared. The following two lines of code returns true

<script type="text/javascript">
        alert([].toString()== [].toString());
        alert([].toString()===[].toString());
</script>

JS whether you want to compare two arrays have the same elements, namely two arrays all the elements are the same, but the order of elements is not necessarily the same. Only you need to first sort the array, and then compare the two arrays are equal.
Compare the following two lines of code:

<script type="text/javascript">
        alert([1,2,3].toString()== [3,2,1].toString());
        alert([1,2,3].sort().toString()== [3,2,1].sort().toString());
</script>

 

Guess you like

Origin www.cnblogs.com/loveyouyou616/p/11276249.html