您好我有一个像跟随的JSON文件:
{"a" :[
{
"b": "name_1",
"c":[
{"d": "value_1", "e": "true"},
{"d": "value_2"},
{"d": "value_3", "e": "true"}
]
},
{
"b": "name_2",
"c":[
{"d": "value_4", "e": "true"},
{"d": "value_5"},
{"d": "value_6"}
]}
]
}
我想删除包含密钥“e”的“c”中的每个对象。
我尝试使用JQ,但我无法获得正确的命令/过滤器......
离我最近的...... https://jqplay.org/s/fDzVed_Isv
答案 0 :(得分:0)
检查你的链接,我看到你为这些删除返回NULL。
您可能希望使用splice
,而不是delete
。
参考:Delete Javascript object item adding null value
否则,您可以使用地图中的地图执行此操作。
var obj = {"a" :[
{
"b": "name_1",
"c":[
{"d": "value_1", "e": "true"},
{"d": "value_2"},
{"d": "value_3", "e": "true"}
]
},
{
"b": "name_2",
"c":[
{"d": "value_4", "e": "true"},
{"d": "value_5"},
{"d": "value_6"}
]}
]
}
//console.log('obj is: ', obj)
//console.log('obj.a is: ', obj.a)
var array = obj.a
var x = array.map(function(eachThing) {
var newArr = eachThing.c
newArr.map(function(eachObj) {
console.log(eachObj)
if (eachObj.e === "true")
console.log('delete this!')
})
})
x
答案 1 :(得分:0)
删除包含您必须键入的键的Object:
jq 'del( .a[].c[] | select(has("e")))'
https://jqplay.org/s/B-JW8wnqG3
感谢@steeldriver在这篇文章中回答了这个问题 https://askubuntu.com/questions/1046215/find-substring-in-file/1046230?noredirect=1#comment1706714_1046230