我需要在nodeJS中删除所有有两个点或有空格或为空的键。
我有这个 JSON:
{
"cmd": [
{
"key:test": "False",
"id": "454",
"sales": [
{
"customer_configuration": {
"key:points": "test value",
"": "empty key",
"some_field": "test"
}
}
]
}
]
}
目标 JSON:
{
"cmd": [
{
"id": "454",
"sales": [
{
"customer_configuration": {
"some_field": "test"
}
}
]
}
]
}
我该怎么做?
最佳答案
您可以使用 for...in
循环创建递归函数来搜索数据,然后删除
特定属性。
var obj = {
"cmd": [{
"key:test": "False",
"id": "454",
"sales": [{
"customer_configuration": {
"key:points": "test value",
"": "empty key",
"some_field": "test"
}
}]
}]
}
function deleteKeys(data) {
for (var i in data) {
if (i.indexOf(':') != -1 || i == '') delete data[i]
if (typeof data[i] == 'object') deleteKeys(data[i]);
}
}
deleteKeys(obj)
console.log(obj)
关于javascript - 删除符合某些条件的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41613731/