I have following code:
var userId = r.response[0].uid;
if (typeof ratings[userId] !== 'undefined'){
ratings[userId].likes = ratings[userId].likes++;
ratings[userId].name = r.response[0].first_name +" "+ r.response[0].last_name;
} else {
ratings[userId] = new Object;
ratings[userId].likes = 1;
ratings[userId].name = r.response[0].first_name +" "+ r.response[0].last_name;
}
So i Have strange thing. When I use console.log(ratings) to see what is in object, it shows me all properties of this object. But when I try to get any property like this:
console.log(ratings[12345]) or console.log(ratings["12345"])
it is undefined. But console.log(ratings)
said me that I have property 12345 (it is an object too). What is wrong in my code and how can I fix it?
If you use numbers as ID for the users, then you have to use
var userId = '12345';
ratings[userId];
or
ratings['12345'];
or
ratings[12345+""]; // this is the last resort, if possibile don't use it!
Keep in mind that pure numbers aren't allowed as JavaScript variable/keys identificator, but if they are coerced to string, then you can use them as key in objects.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.