5

I need to store an object in localStorage - and I know that in order to do so, I have to convert the object into a string. All cool.

My problem is in actually creating the object in the first place: I have two values in sessionStorage that need to be added to the object which is then passed into localStorage. However, when I try to create the object, one value is being stored as the variable name rather than its (numeric) value. Any idea whats going on here?

var siteName = sessionStorage['1'];
var siteID = (+sessionStorage['2']);
var temp = {siteID:siteName};
alert(typeof siteID);
alert(JSON.stringify(temp));

The first alert confirms that siteID is indeed a number type, but the second alert shows that the variable name (siteID) is stored rather than its numeric value.

2 Answers 2

4

This line:

var temp = {siteID:siteName};

...creates an object containing a property called siteId with the value taken from the siteName variable.

If you want the property name to be taken from the siteID variable instead:

var temp = {};
temp[siteID] = siteName;

Or in ES2015 (aka "ES6") you could use the new computed property name syntax:

// ES2015+ only!
var temp = {[siteId]: siteName};

In JavaScript, you can access/create properties on objects in two different but equal ways: Using dotted notation with a literal property name:

obj.foo = "bar";    // Creates a `foo` property on `obj` with the value `"bar"`

...or using bracketed notation and a string:

obj["foo"] = "bar"; // Does the same thing

The keys in object initializers like your var temp = {siteID:siteName}; are always used literally (although they can optionally be in quotes); there's no way with an object initializer to have a key taken from a variable instead. So you have to do it as a two-step process, first create the object, then set the property.

So, if you do

temp[siteID] = siteName;

...the number in siteID will be converted to a string and will become the property name, with the value of siteName being the value.

var temp = {};
var key = 1;
temp[key] = "value";
console.log(temp[1]); // "value"
console.log(temp["1"]); // "value"

(Property names are always strings in JavaScript [for now].)

Sign up to request clarification or add additional context in comments.

2 Comments

Also thanks for the explanation (saw your enedited version first ;) ) - clearly explained what 3 useless tutorials couldn't!
@Squishy: That's great to hear!
2

Change it to this.

var temp = {};

temp[siteName] = siteID;

Or if the typeof test was meant to show the property name, you'd reverse them.

var temp = {};

temp[siteID] = siteName;

But be aware that siteID is considered a String from that point forward.

2 Comments

I think you have the key/value backwards
@Mathletics: Yes, you're right. I was confused by the typeof test, thinking that was meant to be the value. I'll update. Thanks for the hint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.