【问题标题】:How to replace the $ symbol from json object key in nodejs如何从nodejs中的json对象键替换$符号
【发布时间】:2020-09-04 08:43:57
【问题描述】:

我正在尝试从 json 对象(已解析)中的键中删除 $ 符号。我使用Remove special character in json key name in nodejs 这个答案提到的普通 JS 文件,它工作正常。但是我在nodejs中尝试过,因为异步它不能正常工作。最后形成的对象不包含整个修改值。所以我尝试了问题中发布的原始函数,但我得到回调函数未找到错误。我正在使用节点 10.19 版本。有没有其他方法可以从我的 json 对象中删除 $ 符号。请给我一个可行的解决方案。实际上,我正在从 yml 文件中获取输入,该文件在 jenkins 中被转换为 json 字符串。再次在我的代码中我已经解析了它。如果有任何库可以直接将 yml 文件转换为 jenkins 中的 json,那也会有所帮助。

var obj = {

    'blue':{
        'test:"value',
        '$test1':'value1',
        'tiger':'cheetah_growl',
        '$jan':'cool'
    }

}

普通的js文件

var obj_new = { '$name': 'test1', '$auth_users': 'bajali_s' };

console.log("obj.blue",obj.blue.tiger);
var str = obj.blue.tiger;
var res = str.replace("_", " ");
console.log("res",res);

obj.blue.tiger = res;

console.log("obj.blue",obj.blue.$test1);



//const obj1 = {"example1.":"sometext.","example2.":"anothertext."};
const obj2 = {};
console.log(Object.getOwnPropertyNames(obj));

const obj1 = obj_new;
console.log("__",obj1);
/* for (const key of Object.getOwnPropertyNames(obj1)) {
  obj2[key.replace(/[|&;$%@."<>()+,]/g, "")] = obj1[key];
} */

for (const key of Object.getOwnPropertyNames(obj1)) {
  obj2[key.replace(/[|&;$%@."<>()+,]/g, "")] = obj1[key];
}


console.log("==",obj2); 

var newjson = JSON.stringify(obj2);

console.log(newjson);

【问题讨论】:

  • 取出不相关的代码。顶部的 sn-p 也有语法错误。
  • 你的代码很难理解。为什么你有这么多obj-变量?你能举一个更简单的小例子来说明你的问题吗?

标签: javascript node.js json jenkins async.js


【解决方案1】:

这是我为删除键和值中的额外符号所做的操作。我浏览了许多网站并提出了这个程序,希望它可以帮助某人。我也让它变得有点动态。

 var special_char_keys = ["creationType", "vm_location", "nic_location", "vnetlocation", "rg_location"];
        var elements_to_delete = ["blueprint_info", "riglet_info", "quick_links"];


        for (var a = elements_to_delete.length; a >= 0; a--) {
            delete cloudProperties[elements_to_delete[a]];
        }

        var Mainkeys = Object.keys(cloudProperties);
        console.log("Mainkeys", Mainkeys);
        var total_keys_cloudproperties = Mainkeys.length;



        for (var j = total_keys_cloudproperties; j > 0; j--) { // main json loop starts

            for (const key of Object.getOwnPropertyNames(cloudProperties)) { // getting the main json keys

                var obj1 = cloudProperties[key];
                var obj2 = {};
                var obj3 = {};
                var obj4 = {};
                var obj5 = {};
                var formattedarray = [];

                var keys = Object.keys(cloudProperties[key]);
                //console.log("inner keys", keys);
                var tasksToGo = keys.length;

                //console.log("key length",tasksToGo);
                for (var i = tasksToGo; i > 0; i--) {



                    for (const key1 of Object.getOwnPropertyNames(obj1)) {
                        //   var item = obj1.get(key1);  // this will get the value in the json based on key

                        if (Array.isArray(obj1[key1])) { // checking whether the key has value as [{},{}]

                            // console.log("for jsonarray case");
                            var temparray = obj1[key1]; // assigning the array to temparray
                            for (var k = 0; k < temparray.length; k++) { // for loop for getting each object value
                                var obj3 = temparray[k]; // assigning the inner object to a variable
                                var Arraykeys = Object.keys(obj3);
                                // console.log("array inner keys", Arraykeys);
                                var tasksToGo1 = Arraykeys.length;

                                for (var t = tasksToGo1; t > 0; t--) {
                                    for (const key2 of Object.getOwnPropertyNames(obj3)) {

                                        if (special_char_keys.includes(key2)) {
                                            var tempvalue = obj3[key2];
                                            var newvalue = tempvalue.replace("_", " ");
                                            // console.log("newvalue", newvalue);
                                            obj4[key2.replace(/[|&;$%@."<>()+,]/g, "")] = newvalue;

                                        } else {
                                            obj4[key2.replace(/[|&;$%@."<>()+,]/g, "")] = obj3[key2];
                                        }
                                        // obj4[key2.replace(/[|&;$%@."<>()+,]/g, "")] = obj3[key2];


                                    }
                                    if (Arraykeys == tasksToGo1) {
                                        break;
                                    }

                                    temparray[k] = obj4;
                                    // console.log("temparray[k] ======================", temparray[k]);
                                }

                            }
                            obj5[key1] = temparray;
                            cloudProperties[key] = obj5;
                            // console.log("vm case", cloudProperties[key]);
                        } else {
                            // console.log("for normal case");
                            if (special_char_keys.includes(key1)) {
                                var tempvalue = obj1[key1];
                                //  console.log(obj1[key1]);
                                var newvalue = tempvalue.replace("_", " ");
                                // console.log("newvalue", newvalue);
                                obj2[key1.replace(/[|&;$%@."<>()+,]/g, "")] = newvalue;
                                cloudProperties[key] = obj2;


                            } else {
                                obj2[key1.replace(/[|&;$%@."<>()+,]/g, "")] = obj1[key1];
                                cloudProperties[key] = obj2;

                            }


                            //obj2[key1.replace(/[|&;$%@."<>()+,]/g, "")] = obj1[key1];
                            // cloudProperties[key] = obj2;
                        }
                    }

                }
                //console.log("obj2",obj2);

                //console.log("+++++++++++++++++",cloudProperties[key]);


            }
            if (j == total_keys_cloudproperties) {
                //console.log("inside break");
                break;
            }
        }




        console.log("After removing", cloudProperties);

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 2016-12-07
    • 2018-01-09
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多