1

I have two JSON objects and I want to check that one of them contains the other one (Object1 contains Object2)

Both JSON objects are dynamic - I don't know the structure or property names of either objects.

For example, Object1 can look like this:

{
    "propertyA": "value1",
    "propertyB": "value2",
    "propertyC": {
         "objProp1": "objVal1",
         "objProp2": "objVal2"       
    }
    "propertyD": [
       {
         "arrProp1": "arrVal1",
         "arrProp2": "arrVal2"
       },
       {
         "arrProp1": "arrVal3",
         "arrProp2": "arrVal4",
       }
    ]
}

While Object2 could be one of the following:

Option 1:

{
    "propertyA": "value1",      
    "propertyD": [          
       {           
         "arrProp2": "arrVal4"
       }
    ]
}

Option 2:

{
   "objProp1": "objVal1"
}
user2717436
  • 775
  • 1
  • 10
  • 23

1 Answers1

0

I hope this code come to some use. I didn't have much time to check all edge cases or reduce complexity.

I am not sure what logic exactly you have in mind, Therefore I wrote code for following conditions.

  1. If you want to check in depth if object1 contains object2 (all keys and values in object2 can be found in object1) then this code may help you.

function _contains(obj1, obj2) {
    const obj2keys = Object.keys(obj2);

    for (let i = 0; i < obj2keys.length; i++) {
        const key = obj2keys[i];
        if (obj1.hasOwnProperty(key)) {
            if (typeof (obj1[key]) === typeof (obj2[key])) {
                if (typeof (obj1[key]) === 'object') {
                    if (Object.keys(obj1[key]).length !== Object.keys(obj2[key]).length)
                        return false;
                    const res = _contains(obj1[key], obj2[key])
                    if (!res)
                        return false;
                } else {
                    if (obj1[key] !== obj2[key])
                        return false;
                }
            } else {
                return false;
            }
        } else
            return false;
    }
    return true;
}

function contains(object1, object2) {
    let res = false;
    res = _contains(object1, object2);
    const keys = Object.keys(object1);
    for (let index = 0; index < keys.length; index++) {
        const key = keys[index];
        if (res)
            return res;
        else if (typeof (object1[key]) === 'object') {
            res = contains(object1[key], object2);
        }
    }
    return res;
}

const object1 = {
    "propertyA": "value1",
    "propertyB": "value2",
    "propertyC": {
        "objProp1": "objVal1",
        "objProp2": "objVal2"
    },
    "propertyD": [{
            "arrProp1": "arrVal1",
            "arrProp2": "arrVal2"
        },
        {
            "arrProp1": "arrVal3",
            "arrProp2": "arrVal4",
        }
    ]
}

const object2 = {
    "propertyA": "value1",      
    "propertyD": [          
       {           
         "arrProp2": "arrVal4"
       }
    ]
}

console.log(contains(object1, object2));
  1. If you only need to check for keys only, then below code should be enough.

function containsKeys(object1,object2){
    let contains =true;
    const object1Keys = Object.keys(object1);
    const object2Keys = Object.keys(object2);

    for (let index = 0; index < object2Keys.length; index++) {
        const key = object2Keys[index];
        if(!object1.hasOwnProperty(key)){
            contains = false;   
        }
    }
    return contains;
}

const object1 = {
    "propertyA": "value1",
    "propertyB": "value2",
    "propertyC": {
        "objProp1": "objVal1",
        "objProp2": "objVal2"
    },
    "propertyD": [{
            "arrProp1": "arrVal1",
            "arrProp2": "arrVal2"
        },
        {
            "arrProp1": "arrVal3",
            "arrProp2": "arrVal4",
        }
    ]
}

const object2 = {
    "propertyA": "value1",      
    "propertyD": [          
       {           
         "arrProp2": "arrVal4"
       }
    ]
}

console.log(containsKeys(object1, object2));
Moha
  • 1
  • 1