我有一個JSON對象,這樣,它是一個嵌套的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像波紋管沒有運氣
result = _.find(data, function(d) {
return d[0] === "9-10"
//also tried this
// return d._id === "9-10"
});
基本上我試圖找到一個對象,用這個鍵「9-10」, 我有谷歌找到一些解決辦法,但沒有工作。
需要一些建議。
你是不是指'data ['9-10']'? – njzk2
@ njzk2我已經發布了我的解決方案,可以建議哪一個更好的方法? – NomNomNom
如果你的數據真的像你描述的那樣,'data ['9-10']'肯定會起作用。 – njzk2