js 数据比对(含对象) Equals - 戴向天

大家好!我叫戴向天

QQ群:602504799

如若有不理解的,可加QQ群进行咨询了解

使用方式

// class
const equalsClass = new Equals()
cons a = [1,{},2,true]
const b = [1,{},2,false]
const c = [1,{},2,true]
console.log(equalsClass.compare(a,b))
console.log(equalsClass.compare(b,c))
console.log(equalsClass.compare(a,c))
// function
console.log(equals(a,b));
console.log(equals(b,c));
console.log(equals(a,c));

Class 方式

class Equals {

    constructor() {
        return this
    }

    getType(obj, str = null) {
        const typeStr = Object.prototype.toString.call(obj).split(' ')[1]
        const type = typeStr.substring(0, typeStr.length - 1).trim().toLowerCase()
        if (type === 'object' && Object.prototype.toString.call(obj).toLowerCase() === '[object object]' && !obj.length) {
            return str ? 'json' === str : 'json'
        }
        return str ? type === str : type
    }

    compare(obj1, obj2) {
        const obj1Type = this.getType(obj1)
        const obj2Type = this.getType(obj2)
        // 判断类型是否一致
        if (obj1Type !== obj2Type) {
            return false
        }

        // 判断内容是否一致
        if (['string', 'number', 'boolean', 'null', 'undefined'].includes(obj1Type) && obj1 !== obj2) {
            return false
        }

        // 对象判断
        if (['json', 'object'].includes(obj1Type) || Array.isArray(obj1)) {
            const obj1Keys = Object.keys(obj1)
            const obj2Keys = Object.keys(obj2)
            // 判断字段数量是否一致
            if (obj1Keys.length !== obj2Keys.length) {
                return false
            }

            const bool = obj1Keys.map(key => obj2Keys.includes(key)).every(b => b)
            // 判断每一个字段都是一致
            if (!bool) {
                return false
            }

            for (let i = 0; i < obj1Keys.length; i++) {
                const key = obj1Keys[i]
                const bool = this.compare(obj1[key], obj2[key])
                if (!bool) {
                    return false
                }
            }
        }

        // 函数判断
        if (['function', 'asyncfunction'].includes(obj1Type)) {
            return obj1.toString() === obj2Keys.toString()

        }

        return true
    }
}

函数方式

function equals(data, data2) {

    function getType(obj, str = null) {
        const typeStr = Object.prototype.toString.call(obj).split(' ')[1]
        const type = typeStr.substring(0, typeStr.length - 1).trim().toLowerCase()
        if (type === 'object' && Object.prototype.toString.call(obj).toLowerCase() === '[object object]' && !obj.length) {
            return str ? 'json' === str : 'json'
        }
        return str ? type === str : type
    }

    function compare(obj1, obj2) {
        const obj1Type = getType(obj1)
        const obj2Type = getType(obj2)
        // 判断类型是否一致
        if (obj1Type !== obj2Type) {
            return false
        }

        // 判断内容是否一致
        if (['string', 'number', 'boolean', 'null', 'undefined'].includes(obj1Type) && obj1 !== obj2) {
            return false
        }

        // 对象判断
        if (['json', 'object'].includes(obj1Type) || Array.isArray(obj1)) {
            const obj1Keys = Object.keys(obj1)
            const obj2Keys = Object.keys(obj2)
            // 判断字段数量是否一致
            if (obj1Keys.length !== obj2Keys.length) {
                return false
            }

            const bool = obj1Keys.map(key => obj2Keys.includes(key)).every(b => b)
            // 判断每一个字段都是一致
            if (!bool) {
                return false
            }

            for (let i = 0; i < obj1Keys.length; i++) {
                const key = obj1Keys[i]
                const bool = compare(obj1[key], obj2[key])
                if (!bool) {
                    return false
                }
            }
        }

        // 函数判断
        if (['function', 'asyncfunction'].includes(obj1Type)) {
            return obj1.toString() === obj2Keys.toString()

        }

        return true
    }

    return compare(data, data2)

}

你可能感兴趣的:(javascript,前端)