💾deepEqual()

JSvaluefunctionrecursive ⟩ deepEqual()

function deepEqual(a, b) {

    // check if `value` is (non-function) object
    function isNonFunctionObject(value) {
        return typeof value === 'object' && value !== null;
    }

    // 1. same value -> always equal
    if (a === b) return true;

    // 2. a !== b

    // 2.1 (either one is primitive/function) different values -> always unequal
    if(!isNonFunctionObject(a) || !isNonFunctionObject(b)) return false;

    // 2.2 both (non-function, different) objects
    
    // - count properties
    const keysA = Object.keys(a), keysB = Object.keys(b);
    if (keysA.length !== keysB.length) return false;

    // - check property keys/values
    for (const key of keysA) {
        if (!keysB.includes(key) || !deepEqual(a[key], b[key])) return false;
    }

    // - property keys/values all match
    return true;
}

💈範例:

Last updated

Was this helpful?