In AngularJS, compare whether two arrays are the same and angularjs two arrays.

Source: Internet
Author: User

In AngularJS, compare whether two arrays are the same and angularjs two arrays.

Javascript cannot directly use = OR = to determine whether two arrays are equal, no matter whether they are equal or full, the following two lines of JS Code will return false

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

To determine whether the two arrays in JS are the same, you need to convert the array to a string before comparison. The following two lines of code 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 in the two arrays are the same, but the order of elements is not necessarily the same. You only need 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 for determination, [null]. toString () = ''is also returned.

The above section shows you how to compare the two arrays in AngularJS. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.