简体   繁体   English

删除包含特定单词的对象/字段

[英]Remove objects/fields which contains a specific word

I have a JSON file; 我有一个JSON文件; I want to remove all of the fields or objects, whose names are a specific word (lets say "test") and then return the stripped JSON file back; 我想删除名称是特定单词的所有字段或对象(让我们说“测试”),然后将剥离的JSON文件返回; how can I do it in Node.JS? 如何在Node.JS中做到这一点?

Here is an example of my JSON file: 这是我的JSON文件的示例:

{
    "name": "name1",
    "version": "0.0.1",
    "storage": {
        "db": {
            "test": "STRING",
            "tets2": "STRING",

        },
        "test": {
            "test11": "STRING",
            "test2": {
                "test3": "0",
                "test4": "0"
            },
            "test5": {
                "test6": "0",
                "test7": "0"
            }
        },
        "test8": {
            "test9": "STRING",
            "test10": "STRING"
        }
    }
}

The desired output: 所需的输出:

{
    "name": "name1",
    "version": "0.0.1",
    "storage": {
        "db": {            
            "tets2": "STRING",
        },        
        "test8": {
            "test9": "STRING",
            "test10": "STRING"
        }
    }
}

I tried the folloiwng, but I dont know how to use typeof() and check if it is an objectgo deeper in the tree! 我尝试了以下操作,但是我不知道如何使用typeof()并检查它是否在树的更深处! could you please help me in this regard 你能在这方面帮助我吗

var new_json = config;

async.each(Object.keys(config), function(key) {

    if (key == "test") {
        delete new_json[key];
    }

    while (typeof (new_json[key]) == "object") {
        // How can I handle it here

    }
});
console.log("done!");

Below Recursion code will work. 下面的递归代码将起作用。 But you need to list of acceptable fields or not acceptable fields and based on that you need to change the below condition IF you know not acceptable fields then use below conditions. 但是,您需要列出可接受的字段或不可接受的字段,并且如果您知道不可接受的字段然后使用以下条件,则需要更改以下条件。

unAcceptableFields.indexOf(key) > 0



var acceptableFields = ["name","version","storage","db", "test9", "test10","tets2", "test8", "test9", "test10" ];
console.log(removeUnwantedFields(testObject, acceptableFields));

function removeUnwantedFields(jsData,acceptableFields) {
    var key;
    if (jsData) {
        for (key in jsData) {           
            if (acceptableFields.indexOf(key) == -1) {
                delete jsData[key];
            }
            else if(typeof jsData[key] === "object"){
              jsData[key] = removeUnwantedFields(jsData[key],acceptableFields);
            }
        }
    }
    return jsData;
}

Refer this URL http://jsfiddle.net/55x2V/ 引用此URL http://jsfiddle.net/55x2V/

This function should do it: 此函数应执行以下操作:

function clean(obj,target) {
    var tmpobj = obj;
    for (var key in tmpobj) {
        if (key === target) {
            delete obj[key];
        }
        else if (typeof obj[key] === "object") {
            obj[key] = clean(obj[key],target); 
        }
    }
    return obj;
}

called this way: 这样称呼:

json_struct = clean(json_struct,"test")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM