我想从猫鼬的对象的嵌套数组中检索单个对象。我在下面附加了示例json代码。
{
"_id" : ObjectId("1"),
"name" : "xxx",
"votes" : [
{
"actionType" : 11,
"voter" : [
{
"user" : "john",
"pass" : a
},
{
"user" : "david",
"pass" : b
}
]
},
{
"actionType" : 22,
"voter" : [
{
"user" : "john",
"pass" : c
},
{
"user" : "david",
"pass" : d
}
]
}
]
}
预期结果为
{
"user":"john",
"pass":c
}
说明:我要使用actionType = 22和user = john的用户对象
答案 0 :(得分:1)
`var obj = {};
var votes = response.votes;
votes = response.votes[];
for(var i=0; i<votes.length; i++) {
if(votes[i].actionTypes === 22) {
var voter = votes[i].voter;
for(var i = 0; i<voter.length;i++) {
if(voter[i].name === 'john') {
obj.push(voter[i]);
break;
}
}
}
}
`