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

参考的代码

<!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>


提问者:知庸vv 2020-10-14 16:30

个回答

  • qq_慕桂英9497959
    2022-09-13 10:39:52

    真厉害啊