NodeJS - 检查JSON是否包含子JSON

时间:2017-11-16 14:23:24

标签: javascript json node.js

我有两个JSON对象,我想检查其中一个是否包含另一个(Object1包含Object2)

两个JSON对象都是动态的 - 我不知道任何一个对象的结构或属性名称。

例如,Object1可能如下所示:

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

虽然Object2可能是以下之一:

选项1:

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

选项2:

{
   "objProp1": "objVal1"
}

1 个答案:

答案 0 :(得分:0)

我希望这段代码有用。我没有太多时间检查所有边缘情况或降低复杂性。

我不确定你的想法是什么,因此我为下列条件编写了代码。

  1. 如果你想深入检查object1是否包含object2(object2中的所有键和值都可以在object1中找到),那么这段代码可以帮助你。
  2. 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. 如果您只需要检查密钥,那么下面的代码就足够了。
    2. 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));