Home  >  Article  >  Web Front-end  >  Comparative analysis of arrays under AngularJS

Comparative analysis of arrays under AngularJS

高洛峰
高洛峰Original
2017-01-14 10:58:281194browse

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>

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>

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>

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

The above comparative analysis of arrays under AngularJS is all the content shared by the editor. I hope it can give you a reference, and I hope you will support the PHP Chinese website.

For more articles related to the comparative analysis of arrays under AngularJS, please pay attention to the PHP Chinese website!

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