0
var userdata = { 

     data:{ 
            "email": "sdafs@gmail.com",
            "phone":7894561230,
            "opcrmMobile": 57892445
           }
}

here i need to acess phone opcrmMobile and email.

I have

var store= userdata.data.opcrmMobile; 

and

var mail=userdata.data.email;

but when i use console.log(store); and console.log(mail);

i get userdata.data.opcrmMobile like this.

but i need the number that is available in the code

Mitul
  • 3,431
  • 2
  • 22
  • 35
Mohan Gopi
  • 7,606
  • 17
  • 66
  • 117

2 Answers2

1

var userdata = { 
     data:{ 
            "email": "sdafs@gmail.com",
            "phone":7894561230,
            "opcrmMobile": 57892445
           }
}
console.log(userdata.data.email)
console.log(userdata.data.opcrmMobile)

store = userdata.data.opcrmMobile;
mail = userdata.data.email;

console.log('mail : ' + mail);
console.log('store : ' + store);
Mitul
  • 3,431
  • 2
  • 22
  • 35
-1

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.

Community
  • 1
  • 1
Rohit416
  • 3,416
  • 3
  • 24
  • 41
  • 1
    then what's the difference between `userdata.data.opcrmMobile` and `userdata.data["opcrmMobile"]` .. – Anik Islam Abhi Nov 02 '15 at 05:49
  • 2
    @AnikIslamAbhi not really a big difference, it's just, the one with brackets allows you to to access objects inside objects where keys are number just like if you have for example something like this `data={0: 'email', 1: 'phone'}` and in this case, you can't use `data.0` to get the data value of index 0, but you use `data[0]` instead – Dexter Bengil Nov 02 '15 at 05:58
  • 1
    thanks for the info mate @david_smithe i just asked in respect of the op's problem ? like why converting from `userdata.data.opcrmMobile` to `userdata.data["opcrmMobile"]` will solve op's problem – Anik Islam Abhi Nov 02 '15 at 06:13
  • @AnikIslamAbhi, i hope i have done justice with my comment, please let me know if i messed up things? we all are here to help and learn.... – Rohit416 Nov 02 '15 at 06:15
  • i was wanted to bring the point that converting from `userdata.data.opcrmMobile` to `userdata.data["opcrmMobile"]` won't anything here in term of op's problem . nothing else mate @Rohit416 :) – Anik Islam Abhi Nov 02 '15 at 06:18
  • @AnikIslamAbhi ohh, alright, my bad mate, I didn't review the edit history. :D – Dexter Bengil Nov 02 '15 at 15:45
  • Updated answer to something better than earlier. – Rohit416 Nov 02 '15 at 19:26