盯叮町玎仃
2016-06-10 15:51
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Untitled Document</title> </head> <body> <script type="text/javascript"> /* * 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 i = 0, n = arr1.length, countMap1 = {}, countMap2 = {}, t1,t2, t = ['string','boolean','number','undefined','null','window']; for(;i<n;i++){ t1 = typeOf(arr1[i]); t2 = typeOf(arr2[i]); if(countMap1[t1]){ countMap1++; } 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 instanceof Array) r = 'array'; else if(ele === window) r = 'window'; else if(ele instanceof Date) r = 'date'; else r = typeof ele; return r; } for(i = 0,n = t.length;i<n;i++){ if(countMap1[t[i]] !== countMap2[t[i]]){ return false; } } return true; } </script> <script src="testData.js"></script> </body> </html>
检查几次了,找不到哪里出错,另外,这个代码好像没有给数组里的元素类型进行排序,这样不会出错吗?
找到问题在哪里了。
JavaScript深入浅出
281111 学习 · 1020 问题
相似问题