Compare objects to check whether they are equal

         * Compares two objects recursively to check if they are  equal.

         * @param {*} obj1 – The first object to compare.

         * @param {*} obj2 – The second object to compare.

         * @returns {boolean} – True if the objects are deeply equal, otherwise false.

         */

        function compareObj(obj1, obj2) {

            try {

                if (typeof obj1 !== ‘object’ || typeof obj2 !== ‘object’) {

                    return obj1 === obj2;

                }

                const keys1 = Object.keys(obj1).sort();

                const keys2 = Object.keys(obj2).sort();

                if (keys1.join(‘,’) !== keys2.join(‘,’)) {

                    return false;

                }

                for (let key of keys1) {

                    if (!compareObj(obj1[key], obj2[key])) {

                        return false;

                    }

                }

                return true;

            } catch (error) {

                log.error(“error @compareObj”, error);

            }

        }

Leave a comment

Your email address will not be published. Required fields are marked *