问答详情
源自:1-6 编程练习

请大神帮我看看,为何 arraysSimilar([,'3',4],[,3,4]) 弹出为true,好像是for in循环哪里出错了,改成普通for循环就没问题,求大神解答

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb18030">

<title>比较两个数组</title>

</head>

<body>

<script type="text/javascript">  

/*

        * param1 Array

        * param2 Array

        * return true or false

        */

function type(a){

return  a === null ? '[object Null]':Object.prototype.toString.apply(a); //hack ie678

}


function arraysSimilar(arr1, arr2){

if(arr1 instanceof Array && arr2 instanceof Array && arr1.length==arr2.length){


var arr11=[],arr22=[];

for(var i in arr1){

arr11.push(arr1[i]);

arr22.push(arr2[i]);

}


var arrs1=arr11.sort(),arrs2=arr22.sort(),ty1=[],ty2=[];

for(var i in arr1){

//alert( Object.prototype.toString.apply(arr1[i]))

ty1.push(type(arrs1[i]));

ty2.push(type(arrs2[i]));

}

// alert(ty1+'\n'+ty2)


var str1=arrs1.toString();

var str2=arrs2.toString();

var str3=ty1.toString();

var str4=ty2.toString();


if(str1==str2&&str3==str4){

return true

}else{return false}


}else{return false}


}


alert(arraysSimilar([,'3',4],[,3,4]))

</script>

</body>

</html>


提问者:慕仙1417878 2017-12-20 13:48

个回答

  • qq_依寻_03298115
    2017-12-20 16:22:34
    已采纳

    for in 一般用来枚举对象属性