节点js JSON清理

k97glaaz  于 2023-03-04  发布在  其他
关注(0)|答案(5)|浏览(170)

我想删除所有值为N/A,-或空字符串的键,如果其中一个值出现在数组中,我想从数组中删除该项,例如,我的示例输出如下:
{"name":{"first":"Daniel","middle":"N/A","last":"Smith"},"age":45}
我期望的输出如下所示
{"name":{"first":"Daniel","last":"Smith"},"age":45}
我试过这个方法,但似乎不起作用:

function recurse(resp) {
  let data;
  for (var x in resp) {
    data = resp[x]

    if (data === 'null' || data === null || data === '-' || typeof data === 'undefined' || (data instanceof Object && Object.keys(data).length == 0)) {
      delete resp[x];
    }

    if (data instanceof Object) {
      data = recurse(data);
    }
  }

  return resp;
}
dojqjjoe

dojqjjoe1#

我添加了一些额外的检查,似乎对更多嵌套对象工作良好

let obj = {"name":{"first":"Daniel","middle":"N/A","last":"Smith", "innerObject":{"prop1":"random value", "prop2":null, "prop3":"N/A", "prop4":{}}},"age":45};

function recurse(data){
for(let key in data){
        if(data[key] instanceof Object){
            if(Object.keys(data[key]).length == 0){
                delete data[key];
            } else {
                recurse(data[key]);
            }
        } else if(data[key] === 'null' || data[key] == 'N/A' || data[key] === null || data[key] === '-' || typeof data[key] === 'undefined' || (data[key] instanceof Object && Object.keys(data[key]).length == 0)){
            delete data[key];
        }
    }
return data;
}
let newobj = recurse(obj)
console.log(newobj)

如果对你有效,请告诉我!谢谢

uajslkp6

uajslkp62#

它正在工作,只需要添加N/A验证

let obj = {"name":{"first":"Daniel","middle":"N/A","last":"Smith"},"age":45};

function recurse(resp) {
  let data;
  for (var x in resp) {
    data = resp[x]

    if (data === 'null' || data == 'N/A' || data === null || data === '-' || typeof data === 'undefined' || (data instanceof Object && Object.keys(data).length == 0)) {
      delete resp[x];
    }

    if (data instanceof Object) {
      data = recurse(data);
    }
  }

  return resp;
}

let newobj = recurse(obj)
console.log(newobj)
nkhmeac6

nkhmeac63#

为了避免超出最大堆栈大小的错误,需要重构代码以迭代地计算对象。我已经调整了代码以包含@Carlos1232的建议,并迭代对象上的值。

function clean(resp) {
    const queue = [resp];

    while (queue.length) {
        const element = queue.pop();

        for (var x in element) {
            const data = element[x];

            if (data === 'null' || data == 'N/A' || data === null || data === '-' || typeof data === 'undefined' || (data instanceof Object && Object.keys(data).length == 0)) {
                delete element[x];
            }
            
            if (data instanceof Object) {
                queue.push(data);
            }
        }
    }

    return resp;
}
q35jwt9p

q35jwt9p4#

你需要写一个递归函数来遍历嵌套字典的json树,使其成为泛型。我演示了如何使用to键从嵌套字典中删除元素:父密钥和子密钥。

myjson={"name":{"first":"Daniel","middle":"N/A","last":"Smith"},"age":45}
 to_delete=[]
 for key,item in myjson.items():
      if isinstance(item,dict):
          for key2,element in item.items():
              print(key,key2,element)
              if element=='N/A':
                  to_delete.append({'key':key,'key2':key2})
      else:
           print(key,item)

  print(to_delete)

  for item in to_delete:
        print(item)
        del myjson[item['key']][item['key2']]

  print(myjson)
cig3rfwq

cig3rfwq5#

const obj = {"name":{"first":"Daniel","middle":"N/A","last":"Smith"},"age":50}

const jsonclean = (data) => {
Object.keys(data).map(key => {
        if(typeof(data[key]) === 'object'){
                jsonclean(data[key]);
        } else if(data[key] === 'null' || data[key] == 'N/A' || data[key] === null || data[key] === '-' || typeof data[key] === 'undefined'){
            delete data[key];
        }
    })
return data;
}

const newobj = jsonclean(obj)
console.log(newobj)

相关问题