Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

深比较函数deepCompare #31

Open
zuopf769 opened this issue Apr 24, 2023 · 1 comment
Open

深比较函数deepCompare #31

zuopf769 opened this issue Apr 24, 2023 · 1 comment

Comments

@zuopf769
Copy link

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

        function isObject(v) {
            // 对象类型并且不为null
            return typeof v === 'object' && v;
        }

        function deepCompare(obj1, obj2) {

            // 是否相等
            let flag = true;

            // 都是对象类型
            if (isObject(obj1) && isObject(obj2)) {
                // 都是数组
                // 数组长度不相等,肯定不相等
                if (Array.isArray(obj1) && Array.isArray(obj2)) {
                    if (obj1.length !== obj2.length) {
                        flag = false;
                    } else {
                        // 递归对比数组的每一项
                        for (let i = 0; i < obj1.length; i++) {
                            // 如果某一个key不相等,直接退出循环
                            if (!deepCompare(obj1[i], obj2[i])) {
                                flag = false;
                                break;
                            }
                        }
                    }
                // 一个是数组一个是对象,肯定不相等
                } else if ((Array.isArray(obj1) && !Array.isArray(obj2)) || (Array.isArray(obj1) && !Array.isArray(obj2))) {
                    flag = false;
                } else {
                    if (Object.keys(obj1).length !== Object.keys(obj2).length) {
                        flag = false;
                    } else {
                        // 递归对比对象的每一个key
                        for (let key in obj1) {
                            if (obj1.hasOwnProperty(key)) {
                                // 如果某一个key不相等,直接退出循环
                                if(!deepCompare(obj1[key], obj2[key])) {
                                    flag = false;
                                    break;
                                }
                            }
                        }
                    }
                }
            // 一个是对象类型,一个是基本数据类型, 肯定不相等
            } else if ((isObject(obj1) && !isObject(obj2)) || (!isObject(obj1) && isObject(obj2))) {
                flag = false;
            // 都是基本数据类型
            } else {
                if (obj1 !== obj2) {
                    flag = false;
                }
            }

            return flag;
        }

        // const res = deepCompare(1, 1); // true
        // const res = deepCompare(1, '1'); // false
        // const res = deepCompare([1, 2], [1, 2]); // true
        // const res = deepCompare({a: 1, b: 2}, {a: 1}); // false
        // const res = deepCompare([{a: 1, b: 2}, 3], [{a: 1, b: 2}, 3]); // true
        const res = deepCompare([{a: 1, b: 2}, 3], [{a: 1, b: 3}, 3]); // false
        console.log(res);


    </script>
</body>
</html>
@zuopf769
Copy link
Author

飞猪笔试,实现一个比较函数(deepCompare)对传入的两个参数进行深度比较是否相等,综合利用迭代(迭代对象、数组的每一项)、递归(每一项都调用比较函数)
若二者皆为对象,分三种情况比较:1、都为数组2、一数组、一对象3、都为对象;若二者一为对象,一为基本数据类型,则不等;若二者皆为基本数据类型,则根据严格比较判断是否相等。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant