Javascript:通过值

时间:2018-02-24 10:56:49

标签: javascript json

我需要从AJAX调用返回的json字符串中删除元素。

我不确定如何遍历字符串并删除值为NULL的所有元素。

我的json看起来像这样。

[
 {"ID":"27","Agility":"15","Balance":null,"Strength":"37","Physiology":"32"},
 {"ID":"30","Agility":"27","Balance":null,"Strength":null,"Physiology":null},
 {"ID":"34","Agility":null,"Balance":null,"Strength":null,"Physiology":null},
 {"ID":"36","Agility":null,"Balance":null,"Strength":null,"Physiology":null},
 {"ID":"40","Agility":null,"Balance":"20","Strength":null,"Physiology":"34"},
 {"ID":"42","Agility":null,"Balance":"16","Strength":null,"Physiology":null},
 {"ID":"51","Agility":null,"Balance":null,"Strength":"39","Physiology":null}
]

我想我可能会为每个键执行类似的操作,但我更喜欢通用函数:

if(json[index].Strength == null){
    json.splice(index,1);
}

2 个答案:

答案 0 :(得分:2)

您可以使用JSON.parse方法解析json,然后在该数组上使用filter()方法。

const json = '[{"ID":"27","Agility":"15","Balance":null,"Strength":"37","Physiology":"32"},{"ID":"30","Agility":"27","Balance":null,"Strength":null,"Physiology":null},{"ID":"34","Agility":null,"Balance":null,"Strength":null,"Physiology":null},{"ID":"36","Agility":null,"Balance":null,"Strength":null,"Physiology":null},{"ID":"40","Agility":null,"Balance":"20","Strength":null,"Physiology":"34"},{"ID":"42","Agility":null,"Balance":"16","Strength":null,"Physiology":null},{"ID":"51","Agility":null,"Balance":null,"Strength":"39","Physiology":null}]'

const data = JSON.parse(json).filter(o => o.Strength != null)
console.log(data)

如果要删除某些属性值为null的元素,可以在some中使用filter方法。

const json = '[{"ID":"27","Agility":"15","Balance":null,"Strength":"37","Physiology":"32"},{"ID":"30","Agility":"27","Balance":null,"Strength":null,"Physiology":null},{"ID":"34","Agility":null,"Balance":null,"Strength":null,"Physiology":null},{"ID":"36","Agility":null,"Balance":null,"Strength":null,"Physiology":null},{"ID":"40","Agility":null,"Balance":"20","Strength":null,"Physiology":"34"},{"ID":"42","Agility":null,"Balance":"16","Strength":null,"Physiology":null},{"ID":"51","Agility":null,"Balance":null,"Strength":"39","Physiology":null}]'

const data = JSON.parse(json).filter(o => {
  return !Object.keys(o).some(k => o[k] == null)
})
console.log(data)

答案 1 :(得分:1)

  

如果您更喜欢通用功能, Lodash 是最佳选择。

     

PickBy 根据谓词从对象中选取属性   这里的谓词是 Identity ,这意味着从对象中获取非null属性。

var jsonResponse = '[{"ID":"27","Agility":"15","Balance":null,"Strength":"37","Physiology":"32"},{"ID":"30","Agility":"27","Balance":null,"Strength":null,"Physiology":null},{"ID":"34","Agility":null,"Balance":null,"Strength":null,"Physiology":null},{"ID":"36","Agility":null,"Balance":null,"Strength":null,"Physiology":null},{"ID":"40","Agility":null,"Balance":"20","Strength":null,"Physiology":"34"},{"ID":"42","Agility":null,"Balance":"16","Strength":null,"Physiology":null},{"ID":"51","Agility":null,"Balance":null,"Strength":"39","Physiology":null}]';

var responseArr = JSON.parse(jsonResponse);

// Only lines that matter
responseArr = _.map(responseArr, function(obj) {
  return _.pickBy(obj, _.identity);
});

console.log("Array of Objects: ", responseArr);
console.log("JSON: ", JSON.stringify(responseArr));
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>

  

在关闭html页面的标记之前放置上面提到的脚本标记。

     

所以只需使用map遍历整个数组响应,并在数组的每个对象上应用 PickBy ,你就拥有了一个稀疏对象数组。