The way you are accessing the object property should work fine.
var userdata = {
data:{
"email": "sdafs@gmail.com",
"phone": 7894561230,
"opcrmMobile": 57892445
}
};
var store= userdata.data.opcrmMobile;
// or
var store= userdata.data["opcrmMobile"];
console.log(store); // should output "sdafs@gmail.com"
You can still access the properties even if their names are not written as a string literal i.e. "email"
or email
, "phone"
or phone
, nothing mysterious here.
var userdata = {
data:{
email: "sdafs@gmail.com",
phone: 7894561230,
opcrmMobile: 57892445
}
};
console.log(userdata.data.email);
console.log(userdata.data.phone);
console.log(userdata.data.opcrmMobile);
console.log(userdata.data["email"]);
console.log(userdata.data["phone"]);
console.log(userdata.data["opcrmMobile"]);
console.log("show my object properties: " + Object.keys(userdata.data));
Check Output here. There is no celebrity code, just emphasized what you were trying to do.
Besides, it depends upon the use case that whether you need to use the dot (.) or square bracket []
notation to access object properties.
This is a nice brief overview of accessing the object properties with dot .
vs square bracket []
notation.