我必须遍历多级 JSON 并删除某些键

I have to traverse a multilevel JSON and remove certain keys

我有一个 JSON 如下所示。我需要删除数组中提到的一些键 = ["field", "type", "input"]

const result = {
   "condition":"AND",
   "rules":[
      {
         "id":"search_across",
         "operator":"equal",
         "value":1
      },
      {
         "condition":"AND",
         "rules":[
            {
               "id":"Recipe ID",
               "field":"Recipe ID",
               "type":"string",
               "input":"text",
               "operator":"equal",
               "value":"100"
            },
            {
               "condition":"AND",
               "rules":[
                  {
                     "id":"Recipe ID",
                     "field":"Recipe ID",
                     "type":"string",
                     "input":"text",
                     "operator":"equal",
                     "value":"200"
                  }
               ]
            }
         ]
      }
   ]
}

我需要删除第 n 级的密钥。我只能将其从第一级删除(如 JSON 中所示)

您必须递归地迭代对象并搜索它们。

function remove(object, keys) {
    for (var property in object) { //iterate json
        if (object.hasOwnProperty(property)) { //iterate only own properties
            if (keys.includes(property)) { //check if key is one of them
                delete object[property]; //if yes, delete it
            }
            else if (typeof object[property] === "object") { //if is an object, repeat
                remove(object[property], keys);
            }
        }
    }
}

var result = { "condition":"AND", "rules":[ { "id":"search_across", "operator":"equal", "value":1 }, { "condition":"AND", "rules":[ { "id":"Recipe ID", "field":"Recipe ID", "type":"string", "input":"text", "operator":"equal", "value":"100" }, { "condition":"AND", "rules":[ { "id":"Recipe ID", "field":"Recipe ID", "type":"string", "input":"text", "operator":"equal", "value":"200" } ] } ] } ] };

remove(result, ["field", "type", "input"]);
console.log(result);

编辑:如果您想保留原始数据,请将其克隆到新对象中:

function remove(object, keys) {
    for (var property in object) { //iterate json
        if (object.hasOwnProperty(property)) { //iterate only own properties
            if (keys.includes(property)) { //check if key is one of them
                delete object[property]; //if yes, delete it
            }
            else if (typeof object[property] === "object") { //if is an object, repeat
                remove(object[property], keys);
            }
        }
    }
    
    return object;
}

var result = { "condition":"AND", "rules":[ { "id":"search_across", "operator":"equal", "value":1 }, { "condition":"AND", "rules":[ { "id":"Recipe ID", "field":"Recipe ID", "type":"string", "input":"text", "operator":"equal", "value":"100" }, { "condition":"AND", "rules":[ { "id":"Recipe ID", "field":"Recipe ID", "type":"string", "input":"text", "operator":"equal", "value":"200" } ] } ] } ] };
var copy = JSON.parse(JSON.stringify(result));
remove(copy, ["field", "type", "input"]);
console.log(result);
console.log(copy);

您可以创建递归函数,使用 for...in 循环到 delete 属性 在任何级别上按键。

const data = {"condition":"AND","rules":[{"id":"search_across","operator":"equal","value":1},{"condition":"AND","rules":[{"id":"Recipe ID","field":"Recipe ID","type":"string","input":"text","operator":"equal","value":"100"},{"condition":"AND","rules":[{"id":"Recipe ID","field":"Recipe ID","type":"string","input":"text","operator":"equal","value":"200"}]}]}]}

function remove(data, keys) {
  for(let i in data) {
    if(keys.includes(i)) delete data[i];
    else if(typeof data[i] === 'object') remove(data[i], keys)
  }
}

remove(data, ["field", "type", "input"])
console.log(data)