SoFunction
Updated on 2025-04-12

Comparison of whether two arrays are the same in AngularJS

Javascript cannot directly use == or == to determine whether two arrays are equal, whether they are equal or inconsistent, and the following two lines of JS code will return false

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

To determine whether two arrays in JS are the same, you need to convert the array into a string before comparing it. The following two lines of code will return true

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

JS needs to compare whether two arrays have the same elements, that is, all elements of the two arrays are the same, but the order of elements may not be the same. It only needs to sort the array first and then compare whether the two arrays are equal.

<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>

In addition, if [null] is used to judge, you can also return true by [null].toString() === ''.

The above is the comparison of whether the two arrays in AngularJS introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!