Setting array key value pair JavaScript -


so, having issue , life of me cannot seem resolve it. seems basic, cannot understand life of me why code not working.

my issue is, assigning key value pair array, values not assigned. variable scope issue?

here code

function getcookie(cookiename){      var mycookies = []; // cookie jar       var temp = document.cookie.split(";");      var key  = "";      var val  = "";      for(i=0;i<temp.length;i++){          key = temp[i].split("=")[0];          val = temp[i].split("=")[1];          mycookies[key] = val;      }      return mycookies[cookiename]; } 

trim key because cookie strings this:

"__utma=250730393.1032915092.1427933260.1430325220.1430325220.1; __utmc=250730393; __utmz=250730393.1430325220.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); clicks=22; _gat=1; _ga=ga1.2.1032915092.1427933260"

so when split on ; there space before of key names.

function getcookie(cookiename){      var mycookies = []; // cookie jar       var temp = document.cookie.split(";");      var key  = "";      var val  = "";      for(i=0;i<temp.length;i++){          key = temp[i].split("=")[0].trim(); // added trim here          val = temp[i].split("=")[1];          mycookies[key] = val;      }      return mycookies[cookiename]; } 

demo: jsbin


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -