我有数组对象,如果键不匹配,我想删除一些内部对象。
输入:
"configuration" : {
"11111-2222-3333-444--5555" : {
"home1" :
{
"tel" : "125",
"address" : true,
}
},
"2222-3333-44444-5555--66666" : {
"home2" :
{
"tel" : "125",
"address" : true,
}
}
}
我有一个匹配字符串11111-2222-3333-444--5555
预期出局:
"configuration" : {
"11111-2222-3333-444--5555" : {
"home1" :
{
"tel" : "125",
"address" : true
}
}
}
答案 0 :(得分:1)
使用_.pick()
获取您想要的密钥:
var data = {"configuration":{"11111-2222-3333-444--5555":{"home1":{"tel":"125","address":true}},"2222-3333-44444-5555--66666":{"home2":{"tel":"125","address":true}}}};
var searchKey = '11111-2222-3333-444--5555';
var result = {
configuration: _.pick(data.configuration, searchKey)
};
console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
&#13;
答案 1 :(得分:0)
您可以循环显示按键并删除您不想要的按键:
start = 0
end = 1024
i = start
while True:
print(i)
if i==end:
increment = -1
elif i==start:
increment = 1
i += increment
但是,如果您只删除除一个键之外的所有键,则不需要这样做。为了简化它,你可以这样做:
let o = {
configuration: { /* etc. */ }
}
for(let key in o.configuration) {
if(key !== '11111-2222-3333-444--5555') {
delete o[key]
}
}