通过键查找json对象,并将其替换为复杂/嵌套json数组中的其他对象

时间:2019-04-02 12:01:34

标签: javascript angular lodash jsonpath

我正在尝试通过属性/键从复杂/嵌套的json数组中找到json对象,并将其替换为有角项目中的其他json对象。

我用lodash通过键查找json对象,但是对象的json路径可以在json数组上的任意位置。

这是我的示例json数组:

{
  "type": "form",
  "title": "title",
  "name": "name",
  "display": "form",
  "path": "path",
  "components": [
    {
      "mask": false,
      "tableView": true,
      "alwaysEnabled": false,
      "label": "",
      "rows": [
        [
          {
            "components": [
              {
                "key": "key1",
                "valueProperty": "value",
                "selectedKey": "ValueKey"
              }
            ]
          }
        ],
        [
          {
            "components": [
              {
                "components": [
                  {
                    "key": "key2",
                    "valueProperty": "value",
                    "selectedKey": "ValueKey"
                  }
                ],
                "allowMultipleMasks": false,
                "showWordCount": false,
                "showCharCount": false,
                "alwaysEnabled": false,
                "type": "textfield",
                "input": true,
                "widget": {
                  "type": ""
                }
              }
            ]
          }
        ],
        [
          {
            "components": [
              {
                "labelPosition": "left-left",
                "allowMultipleMasks": false,
                "showWordCount": false,
                "showCharCount": false,
                "alwaysEnabled": false,
                "input": true,
                "widget": {
                  "type": ""
                }
              }
            ]
          }
        ]
      ],
      "header": [],
      "numCols": 2
    }
  ]
}

我正在尝试查找整个json对象(如果它包含“ selectedkey”属性)并替换为其他对象。

预期结果:

Json object { "key": "key1", "valueProperty": "value", "selectedKey": "ValueKey" } should be replaced with { "key": "key1", "valueProperty": "value", "selectedKey": "ValueKey" } 

注意:Json对象可以出现n次,并且该对象的json路径可以在json数组中的任意位置。

1 个答案:

答案 0 :(得分:1)

更新:如果只想检查该属性是否存在,则可以对对象进行字符串化并进行includes检查,如下所示:

function containsKey(obj, key) {
    return JSON.stringify(obj).includes(key);
}

//OR

function containsKey(obj, key) {
    return JSON.stringify(obj).indexOf(key) !== -1;
}

您可以使用它用条件块替换整个对象:

if (containsKey(obj, 'selectedKey')) {
    obj = otherObj;
}

其他解决方案::我还调整了我以前的解决方案,该解决方案替换了selectedKey只是为了检查密钥。此解决方案的优点是,该函数将在找到selectedKey之后立即返回,而不是JSON.stringify,后者将遍历每个属性和值。

function containsKey(obj, targetKey) {

    // if the obj is an array, iterate it and call containsKey
    if (Array.isArray(obj)) {
        for (let i = 0; i < obj.length; i++) {
            return containsKey(obj[i], targetKey);
        }
    } else {
        // if it's not an array, it will be an obj
        let keys = Object.keys(obj);
        let key, value;

        // iterate the keys of the object
        for (let i = 0; i < keys.length; i++) {
            key = keys[i];
            value = obj[key];

            // if we find the target key, return true
            // otherwise, call containsKey on any objects
            if (key === targetKey) {
                return true;
            } else if (typeof value === 'object') {
                return containsKey(value, targetKey);
            }
        }
    }

    /* by this time we've checked every property of the object,
       so we know it does not contain the property or it would
       have returned true */
    return false;
}