1

I am using Laravel and Mongodb.

My JSON output:

[{"_id":"58aabc59836a0656da4e4c71",
"userid":"KT0001",
"username":"Ashok kumar",
"email":"[email protected]",
"mobileno":8015681377,
"manager_mail":"[email protected]",
"roleid":2,
"role":  {"_id":"58996226d31868ea68c7b52c",**"roleid":2,"rolename":"Admin"**}
},
]

I want to get the role id and role name, i can get the other details except rolename, i tried retrieving it with the following but no result.

for(i=0;i<userdata.length;i++)
         { 
            $('<option/>', {
              value: userdata[i].roleid,
              html: userdata[i][role].rolename
            }).appendTo('select#userrole');
         };

Please help?

1
  • change this: userdata[i][role].rolename by userdata[i]['role'].rolename and try again. Commented Feb 22, 2017 at 8:59

2 Answers 2

3

use like

 userdata[i].role.rolename
Sign up to request clarification or add additional context in comments.

Comments

0

change this:

userdata[i][role].rolename

to this:

userdata[i]['role'].rolename

'role' key is a string and you are using it as a variable name.

4 Comments

you don't think so this little correction, you also suggest by comment ..!!
@SoniVimal what could be the answer in your opinion. This is where i found issue and it should work if it is.
Thank you all, both userdata[i].role.rolename and userdata[i]['role'].rolename working, what is the proper way?
both are absolutely valid but chosen one can create issues if one can try to minify the js.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.