循环在JSON对象中并删除特定的子对象

时间:2021-07-27 22:51:51

I have a json object from which i want to remove child having a key "errMsg".

我有一个json对象,我想删除有一个键“errMsg”的孩子。

input JSON : {"info":[{"errorMsg":"Unable to find Vendor ","c2v":"some text"},{"errorMsg":"Unable to find Vendor ","c2v":"Some text"},{"errorMsg":"Unable to find Vendor","c2v":" Some text"},{"id":"1038578481","ven":"DEMOMA","c2v":" Some text"}]}

result i want is the JSON should only have the child which doesn't have "errorMsg" in it.

我想要的结果是JSON应该只有没有“errorMsg”的孩子。

output JSON i want : {"info":[{"id":"1038578481","ven":"DEMOMA","c2v":" Some text"}]}

code i used

我用的代码

jsonKeyInfo = stringToJson(form.response); 
for(var i in jsonKeyInfo.info){
            if(jsonKeyInfo.info[i].errorMsg){
                errMsg = jsonKeyInfo.info[i].errorMsg;
                jsonKeyInfo.info.splice(i,1);
                err++;
            //  delete jsonKeyInfo.info[i];
            }
        }

Not working for me.

不适合我。

2 个解决方案

#1


3  

try this it will filter your array and will result your needed data as result

试试这个它会过滤你的数组,并将结果你需要的数据

var jsonData = {"info":[{"errorMsg":"Unable to find Vendor ","c2v":"some text"},{"errorMsg":"Unable to find Vendor ","c2v":"Some text"},{"errorMsg":"Unable to find Vendor","c2v":" Some text"},{"id":"1038578481","ven":"DEMOMA","c2v":" Some text"}]};

var result = jsonData.info.filter(i=>!i.errorMsg)

console.log(result)

To assing it back use

协助它回来使用

jsonData.info = result;

jsonData.info =结果;

try this in your console :) enjoy

在你的控制台尝试这个:)享受

#2


0  

obj = {"info":[
  {"errorMsg":"Unable to find Vendor ","c2v":"some text"},
  {"errorMsg":"Unable to find Vendor ","c2v":"Some text"},
  {"errorMsg":"Unable to find Vendor","c2v":" Some text"},
  {"id":"1038578481","ven":"DEMOMA","c2v":" Some text"}
  ]}
  
  var changed = false;

obj.info = obj.info.filter((el)=> !( el.hasOwnProperty("errorMsg") && (changed = true) ));
console.log(obj);
console.log("Obj.info changed? " + changed);

changed = false;
obj2 = [
{"id":"1038578481","ven":"DEMOMA","c2v":" Some text"},
{"id":"1038578481","ven":"DEMOMA","c2v":" Some text"}
];

obj2.filter((el)=> !( el.hasOwnProperty("errorMsg") && (changed = true) ));
console.log("Obj2 changed? " + changed);

#1


3  

try this it will filter your array and will result your needed data as result

试试这个它会过滤你的数组,并将结果你需要的数据

var jsonData = {"info":[{"errorMsg":"Unable to find Vendor ","c2v":"some text"},{"errorMsg":"Unable to find Vendor ","c2v":"Some text"},{"errorMsg":"Unable to find Vendor","c2v":" Some text"},{"id":"1038578481","ven":"DEMOMA","c2v":" Some text"}]};

var result = jsonData.info.filter(i=>!i.errorMsg)

console.log(result)

To assing it back use

协助它回来使用

jsonData.info = result;

jsonData.info =结果;

try this in your console :) enjoy

在你的控制台尝试这个:)享受

#2


0  

obj = {"info":[
  {"errorMsg":"Unable to find Vendor ","c2v":"some text"},
  {"errorMsg":"Unable to find Vendor ","c2v":"Some text"},
  {"errorMsg":"Unable to find Vendor","c2v":" Some text"},
  {"id":"1038578481","ven":"DEMOMA","c2v":" Some text"}
  ]}
  
  var changed = false;

obj.info = obj.info.filter((el)=> !( el.hasOwnProperty("errorMsg") && (changed = true) ));
console.log(obj);
console.log("Obj.info changed? " + changed);

changed = false;
obj2 = [
{"id":"1038578481","ven":"DEMOMA","c2v":" Some text"},
{"id":"1038578481","ven":"DEMOMA","c2v":" Some text"}
];

obj2.filter((el)=> !( el.hasOwnProperty("errorMsg") && (changed = true) ));
console.log("Obj2 changed? " + changed);