Home > Web Front-end > JS Tutorial > Compare two arrays to see if they are the same in AngularJS

Compare two arrays to see if they are the same in AngularJS

高洛峰
Release: 2017-01-14 11:01:43
Original
1244 people have browsed it

Javascript cannot directly use == or === to determine whether two arrays are equal, whether they are equal or congruent. The following two lines of JS code will return false

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

To determine whether two arrays in JS are the same, you need to convert the arrays into strings first and then compare them. The following two lines of code will return true

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

JS needs to compare two arrays to see if they have the same elements, that is, all elements of the two arrays are the same, but the order of the elements is not necessarily consistent. All you need to do is sort the arrays first, and then compare the two arrays to see if they 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>
Copy after login

In addition, [null] can also be used to judge [null].toString() === '' will also return true.

The above is what the editor introduces to you to compare whether two arrays are the same in AngularJS. 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. I would also like to thank you all for your support of the PHP Chinese website!

For more related articles on comparing whether two arrays are the same in AngularJS, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template