<!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 typeResult(obj) { if (typeof (obj) == "object") { var dataType = Object.prototype.toString.apply(obj); dataType = dataType.substring(8, dataType.length - 1); return dataType; } else { return typeof (obj); } } function arraysSimilar(arr1, arr2) { // 判断变量是否为数组 if (!(arr1 instanceof Array && arr2 instanceof Array)) { return false; } // 判断数组长度是否一致 if (arr1.length !== arr2.length) return false; // 判断数组是否相似 // 新建数组存放数据类型,将arr1的变量类型存放在数组中 var typeArr = new Array(); for (var i = 0; i < arr1.length; i++) { typeArr[i] = typeResult(arr1[i]); } // 判断arr2数组内每个数据的数据类型是否存在 for (var i = 0; i < arr2.length; i++) { var dataType = typeResult(arr2[i]); if (typeArr.indexOf(dataType) < 0) { return false; } } return true; } </script> <script src="testData.js"></script></body></html>
<!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 typeResult(obj) { if (typeof (obj) == "object") { var dataType = Object.prototype.toString.apply(obj); dataType = dataType.substring(8, dataType.length - 1); return dataType; } else { return typeof (obj); } } function arraysSimilar(arr1, arr2) { // 判断变量是否为数组 if (!(arr1 instanceof Array && arr2 instanceof Array)) { return false; } // 判断数组长度是否一致 if (arr1.length !== arr2.length) return false; // 判断数组是否相似 // 新建数组存放数据类型,将arr1的变量类型存放在数组中 var typeArr = new Array(); for (var i = 0; i < arr1.length; i++) { typeArr[i] = typeResult(arr1[i]); } // 判断arr2数组内每个数据的数据类型是否存在 for (var i = 0; i < arr2.length; i++) { var dataType = typeResult(arr2[i]); if (typeArr.indexOf(dataType) < 0) { return false; } } return true; } </script> <script src="testData.js"></script></body></html>