Reputation: 816
I'm checking to see if a key exists in an object from another object so I am doing something like this:
var user = {
'email' : 'dothis',
'derp' : 'yo'
};
And then I'm checking to see if it exists within this:
var cookies = {
'email':'[email protected]'
}
I'm checking like this:
for(var key in cookies) {
if(user[key]){
// do this
}
}
But no matter what I do it will return undefined. However it does work when I just do:
user['email'];
Which is correct.
EDIT: Let me add on to exactly what I'm doing.
So I'm getting all the cookies in the browser using this function:
getAllCookies : function(){
var pairs = document.cookie.split(";");
var cookies = {};
for (var i=0; i<pairs.length; i++){
var pair = pairs[i].split("=");
cookies[pair[0]] = unescape(pair[1]);
}
return cookies;
}
From that I get the object cookies.
Upvotes: 3
Views: 79
Reputation: 224862
There are spaces in a cookie string, so you need to trim the name first, or just split with whitespace:
var pairs = document.cookie.split(/\s*;\s*/);
Upvotes: 2