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