我有数组对象,如果键不匹配,我想删除一些内部对象。

输入:

"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()获取所需的密钥:



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>

07-24 09:22