1 /* 2 * param1 Array 3 * param2 Array 4 * return true or false 5 */ 6 function arraysSimilar(arr1, arr2){ 7 if (arr1 instanceof Array){ 8 return false; 9 } 10 if (arr2 instanceof Array){ 11 return false; 12 } 13 if (arr1.length !== arr2.length){ 14 return false; 15 } 16 17 var i = 0, 18 n = arr1.length, 19 countMap1 = {}, 20 countMap2 = {}, 21 t1, t2, 22 TYPES = [‘string‘, ‘boolean‘, ‘number‘, 23 ‘undefined‘, ‘null‘, ‘function‘, 24 ‘data‘, ‘window‘]; 25 26 for (; i < n; i++){ 27 t1 = typeOf(arr1[i]); 28 t2 = typeOf(arr2[i]); 29 if (countMap1[t1]){ 30 countMap1[t1]++; 31 } else { 32 countMap1[t1] = 1; 33 } 34 if (countMap2[t2]){ 35 countMap2[t2]++; 36 } else { 37 countMap2[t2] = 1; 38 } 39 } 40 41 function typeOf(ele){ 42 var r; 43 if (ele === null) r = ‘null‘; 44 else if (ele == window) r = ‘window‘; 45 else if (ele instanceof Array) r = ‘array‘; 46 else if (ele instanceof Date) r = ‘date‘; 47 else r = typeof ele; 48 return r; 49 } 50 51 for (i = 0, n = TYPES.length; i < n; i++){ 52 if (countMap1[TYPES[i]] !== countMap2[TYPES[i]]){ 53 return false; 54 } 55 } 56 57 return true; 58 }
原文:http://www.cnblogs.com/veryperfect/p/5097310.html