JavaScript simple implementation of the two array similarity test

Source: Internet
Author: User

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"Xml:lang= "en"><Head>    <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8">    <title>JS detects if two arrays are similar</title></Head><Body>    <Script>        //the member types in the array are the same, and the order can be different. For example [1, True] is similar to [FALSE, 2].          //the length of the array is consistent.         //the type of judgment range that needs to be distinguished: String, Boolean, number, undefined, null, function, date, window.        functionarrayssimilar (arr1, arr2) {//Judging Boundaries            if (!(arr1instanceofArray)|| !(arr2instanceofArray)) {                return false; }            //Judging length            if(Arr1.length!=arr2.length)return false; varI= 0, N=Arr1.length, CountMap1={}, CountMap2={}, T1, T2, TYPES= ['string', 'Boolean', ' Number', 'undefined', NULL, 'function', 'Date', 'window'];  for(; I<N; I++) {T1=typeOf (Arr1[i]); T2=typeOf (Arr2[i]); if(Countmap1[t1]) {COUNTMAP1[T1]++; }Else{COUNTMAP1[T1]= 1; }                if(Countmap2[t2]) {COUNTMAP2[T2]++; }Else{Countmap2[t2]= 1; }            }            functiontypeOf (ele) {varR; if(Ele=== NULL) R= 'NULL'; Else if(EleinstanceofArray) R= 'Array'; Else if(Ele===window) R= 'window'; Else if(EleinstanceofDate) R= 'Date'; ElseR= typeofEle; returnR; }             for(i= 0; I<types.length; I++) {                if(Countmap1[types[i]]!=Countmap2[types[i]])return false; }            return true; } document.write (Arrayssimilar ([1,true], [false, 2])); </Script></Body></HTML>
The member types in the array are the same, and the order can be different. For example [1, True] with [false, 2] is similar to the length of the array of the judging range of the same type, need to differentiate: String, Boolean, number, undefined, null, function, date, window.

JavaScript simple implementation of the two array similarity test

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.