斑猪拖拖
2020-02-19 13:59
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb18030">
<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){
if(arr1.length==arr2.length){
a1=arr1.sort();
a2=arr2.sort();
for(i=0,i<arr1.length,i++){
if(Object.prototype.toString.apply(a1[i]) == Object.prototype.toString.apply(a2[i])){
return true;
}
}
}else{
return false;
}
}else{
return false;
}
}
</script>
<script src="testData.js"></script>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb18030">
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
/*
* param1 Array
* param2 Array
* return true or false
*/
var allList = []
function arraysSimilar(arr1, arr2){
let typeList1 = getTypeList(arr1)
let typeList2 = getTypeList(arr2)
return (typeList1.length === allList.length) && (typeList2.length === allList.length)
}
function getTypeList(arr) {
let typeList = []
arr.forEach(item => {
let type = Object.prototype.toString.call(item)
if (typeList.indexOf(type) < 0) {
typeList.push(type)
}
if (allList.indexOf(type) < 0) {
allList.push(type)
}
})
return typeList
}
</script>
<script src="testData.js"></script>
</body>
</html>
if(arr1 instanceof Array&& arr2 instanceof Array) {...}else{return false}
这句话的意思是如果arr1和arr2都是Array的实例的话就执行...,否则返回false。这样写的话如果两个不是Array就会返回false.
JavaScript深入浅出
281111 学习 · 1020 问题
相似问题