如何找到"哪里"没有键名

时间:2017-05-05 08:11:59

标签: json lodash

我有一个像这样的json对象,它是一个嵌套的json对象:

{
    "9-10": {
        "users": {
            "9": {
                "userID": 9,
                "name": "Cunlay Zheng",
                "avatarURL": "{\"small\":\"http://bs-staging.moselo.com/images/users/small/user9-1492746734\",\"medium\":\"http://bs-staging.moselo.com/images/users/medium/user9-1492746734\",\"large\":\"http://bs-staging.moselo.com/images/users/large/user9-1492746734\"}",
                "roomID": "9-10",
                "token": "0c4ea3da77d3c98107fd11c0c7c84ad8",
                "socketID": "/moselo#Xa3cWnM4kwQkpijfAAAD"
            },
            "10": {
                "userID": 10,
                "name": "Dominic Vedericho",
                "avatarURL": "{\"small\":\"http://bs-staging.moselo.com/images/users/small/user10-1492747647\",\"medium\":\"http://bs-staging.moselo.com/images/users/medium/user10-1492747647\",\"large\":\"http://bs-staging.moselo.com/images/users/large/user10-1492747647\"}",
                "roomID": "9-10",
                "token": "7f206d64fdfc71af4a783ab47516ef60",
                "socketID": "/moselo#BW8boMD4rA1dhlCzAAAE"
            }
        }
    },
      "11-12": {
        "users": {
            "9": {
                "userID": 11,
                "name": "Cunlay Zheng",
                "avatarURL": "{\"small\":\"http://bs-staging.moselo.com/images/users/small/user9-1492746734\",\"medium\":\"http://bs-staging.moselo.com/images/users/medium/user9-1492746734\",\"large\":\"http://bs-staging.moselo.com/images/users/large/user9-1492746734\"}",
                "roomID": "9-10",
                "token": "0c4ea3da77d3c98107fd11c0c7c84ad8",
                "socketID": "/moselo#Xa3cWnM4kwQkpijfAAAD"
            },
            "10": {
                "userID": 12,
                "name": "Dominic Vedericho",
                "avatarURL": "{\"small\":\"http://bs-staging.moselo.com/images/users/small/user10-1492747647\",\"medium\":\"http://bs-staging.moselo.com/images/users/medium/user10-1492747647\",\"large\":\"http://bs-staging.moselo.com/images/users/large/user10-1492747647\"}",
                "roomID": "9-10",
                "token": "7f206d64fdfc71af4a783ab47516ef60",
                "socketID": "/moselo#BW8boMD4rA1dhlCzAAAE"
            }
        }
    }
}

尝试使用_filter和_find像bellow一样没有运气

 result = _.find(data, function(d) {
     return  d[0] === "9-10"

    //also tried this
    // return d._id === "9-10"

}); 

基本上我想用这个键找到一个对象" 9-10", 我有谷歌找到一些工作,但没有工作。

需要一些建议。

3 个答案:

答案 0 :(得分:1)

它是一个对象,所以你不应该使用索引,例如–2147483648 to 2147483647 。您应该能够简单地使用密钥,就像这样 -

0

答案 1 :(得分:0)

试试这个result = _.get(data,'9-10')result = _.get(data,'9-10', defaultValue)

以防您需要获取其他值:

users = _.get(data,['9-10','users']);

nine = _.get(data,['9-10','users', '9']);

很确定你得到的模式。

以下是_.get:https://lodash.com/docs/#get

上文档的链接

答案 2 :(得分:0)

我已经用这种方法解决了这个问题,我不确定这是否是最好的方法,但这个解决方案正在发挥作用。

   var r = _.pickBy(data, function(value, key) {
                    return _.startsWith(key, "9-10");
                });
这是帮助。