I have some JSON that looks, basically, like this:
{
"inactive_user": {
"name": "A",
"id": "1",
"dob": "01011990",
},
"active_staff": {
"id": "B",
"dob": "2",
},
"active_student": {
"name": "C",
"pid": "3",
"dob": "01011990",
},
"no_securityquestion": {
"name": "D",
"pid": "4",
"dob": "01011990",
}
}
for (var parent in userObject){
if(userObject.parent.pid === pid){
console.log(userObject.parent);
reply(userObject.parent);
}
}
To reference a dynamic key in an object, you must use the bracket notation.
for (var parent in userObject){
if(userObject[parent].pid === pid){
console.log(userObject[parent]);
reply(userObject[parent]);
}
}
Also, I see in your data that you have some entries where the id is not pid
but simply id
; I don't know if you want them to be treated the same way as pid
, but if it's the case, you should update your condition accordingly.