简体   繁体   中英

Get value from object

I am passing a string to a function, which will use this string to create a session storage element like this :

fun("key","value");

function fun(string, data){
  var to_session = {string:data};
  sessionStorage.setItem(string, JSON.stringify(to_session));
}

But in session storage instead of storing it as {"key":"value"} its stored as {"string":"value"}

Where am I going wrong ?

You could do it like this:

function fun(string, data){
  var to_session = {};
  to_session[string] = data;
  sessionStorage.setItem(string, JSON.stringify(to_session));
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM