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

报错:array is undefined 求指正**

<!DOCTYPE html>
<head>
    <meta charset = 'utf-8'>
    <title>判断数组相似</title>
</head>
<body>
    <div>
        <h1>Hello World!</h1>
    </div>
    <script type = 'text/javascript'>
        /*首先定义一个函数*/
        function arraysSimilar(arr1,arr2){
            if(!(arr1 instanceof array)||!(arr2 instanceof array))
                return false;
            if(arr1.length!==arr2.length)
                return false;
            var i,
                arr1Type = {},
                arr2Type = {};
            for(i=0;i<arr1.length;i++)
                {
                     if(typeof(arr1[i])){
                         arr1Type[i] = typeof(arr1[i]);}
                     if(typeof(arr2[i])){
                         arr2Type[i] = typeof(arr2[i]);}
                }
            arr1Type.sort();arr2Type.sort();
            if(arr1Type.sort().join() === arr2Type.sort().join()){
                return true;}
                else{return false;}
            }
            arr1 = [1,5,undefined,true],
            arr2 = [2,false,8];
            arraysSimilar(arr1,arr2);
        /*第一步判断是否都为数组*/
        /*第二步判断长度是否相等*/
        /*第三步新建两个数组放入数据类型*/
        /*第四步将数组排序变成字符串然后比较*/
    </script>
</body>

初学,所以暂时没有按照题目要求严格区分数据类型。

报错:array is undefined

代码应该还有一些问题,求大神指正**

提问者:慕容6447874 2018-09-09 15:12

个回答

  • 慕侠2155777
    2018-12-22 09:36:09

    js区分大小写 Array是关键字  var a 和 var A是两个变量

  • 慕容6447874
    2018-09-17 10:06:57

    <!DOCTYPE html>
    <head>
        <meta charset = 'utf-8'>
        <script src ='testData.js'></script>
        <title>判断数组相似</title>
    </head>
    <body>
        <div>
            <h1>Hello World!</h1>
        </div>
        <script>
            function arraysSimilar(arr1,arr2){
            /*第一步判断是否都为数组*/
            /*第二步判断长度是否相等*/
            /*第三步新建两个数组放入数据类型*/
            /*第四步将数组排序变成字符串然后比较*/
                var i,
                arr1Type = [],
                arr2Type = [];
                if(!(arr1 instanceof Array) || !(arr2 instanceof Array))
                    return false;
                if(arr1.length!==arr2.length)
                    return false;
                for(i=0;i<arr1.length;i++)
                    {arr1Type.push(typeof(arr1[i]));
                     arr2Type.push(typeof(arr2[i]));}
                if(arr1Type.sort().join() === arr2Type.sort().join())
                    {return true;}
                else{return false;}
                }
        </script>

  • 念兹在兹X
    2018-09-12 15:38:31

    arr1 instanceof arrayarr2 instanceof array中的'array'改为'Array',数组对象写错了呢

  • menghuanbaolei
    2018-09-10 14:06:47

    <!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         */                 // 类型检测        let typing = function(el) {            return Object.prototype.toString.apply(el).toLowerCase().replace(/\S+\s|\]/g, '')        }                 function arraysSimilar(arr1, arr2){        	// arr1/arr2 无效值判断            if (!arr1 || !arr2) {            	return false;            }            // 是否为数组            if (typing(arr1) != 'array' || typing(arr2) != 'array') {            		if (arr1 != arr2) {            			return false;            		}            } else {            	/*长度判断*/            	if (arr1.length != arr2.length) {            		return false;            	}            	let _arr1 = arr1.map(item => typing(item));            	let _arr2 = arr2.map(item => typing(item));            	_arr1.sort();            	_arr2.sort();            	/*类型判断*/            	for(let i = 0; i < _arr1.length; i++) {            		if (_arr1[i] != _arr2[i]) {            			return false;            		}            	}            }            return true;        }    </script>    <script src="testData.js"></script></body></html>


  • menghuanbaolei
    2018-09-10 14:06:17

    <!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         */                 // 类型检测        let typing = function(el) {            return Object.prototype.toString.apply(el).toLowerCase().replace(/\S+\s|\]/g, '')        }                 function arraysSimilar(arr1, arr2){        	// arr1/arr2 无效值判断            if (!arr1 || !arr2) {            	return false;            }            // 是否为数组            if (typing(arr1) != 'array' || typing(arr2) != 'array') {            		if (arr1 != arr2) {            			return false;            		}            } else {            	/*长度判断*/            	if (arr1.length != arr2.length) {            		return false;            	}            	let _arr1 = arr1.map(item => typing(item));            	let _arr2 = arr2.map(item => typing(item));            	_arr1.sort();            	_arr2.sort();            	/*类型判断*/            	for(let i = 0; i < _arr1.length; i++) {            		if (_arr1[i] != _arr2[i]) {            			return false;            		}            	}            }            return true;        }    </script>    <script src="testData.js"></script></body></html>