我有数组对象,如果键不匹配,我想删除一些内部对象。
输入:
"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
}
}
}
最佳答案
使用_.pick()
获取你想要的 key :
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>
关于javascript - 如何通过检查键来过滤对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44834456/