<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb18030"> <title>Untitled Document</title> </head> <body> <script type="text/javascript"> /*string boolean. number,underfined null function date window * param1 Array * param2 Array * return true or false */ function arraysSimilar(arr1, arr2){ if(!(arr1 instanceof Array) || !(arr2 instanceof Array)){ return false } if(arr1.length!==arr2.length){ return false } var type=["string",'boolean','numbwe','undefined','null','function','date','window']; var i = 0, t1,t2, countMap1={}, countMap2={}; for (i;i<arr1.length;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 } } function typeOf(ele){ var r; if(ele===null) r="null"; else if(ele===window) r='window'; else if(ele instanceof Date) r='date'; else if(ele instanceof Array) r='array'; else r= typeof ele return r; } for(i=0;i<type.length;i++){ if(countMap1[type[i]]!==countMap2[type[i]]){ return false } } return true } </script> <script src="testData.js"></script> </body> </html>
明白了~谢谢
你自己打的代码 自己都不知道吗 看判断条件,这两个数组,内容长度,内容类型,内容类型出现次数不同则为false;arr1=[1,'str',[1,2]];arr2=[1,[1,2],'str2']这俩就true,把arr2中的1 改为‘a'就为false了