Subjective Effect
Subjective Effect

Reputation: 1465

Concatenating a value onto the end of an object property?

I'm saving user preferences using localStorage, like this:

 choicesObject = {          //put values in an object
    "measure1"      : $("#m1").is(':checked'),
    "measure2"      : $("#m2").is(':checked'),
    "measure3"      : $("#m3").is(':checked'),
    "measure4"      : $("#m4").is(':checked'),
    "measure5"      : $("#m5").is(':checked'),
    "measure6"      : $("#m6").is(':checked'),
    "measure7"      : $("#m7").is(':checked'),
    "measure8"      : $("#m8").is(':checked')
}
localStorage.setItem("choices", JSON.stringify(choicesObject));

Then I'm getting them back out like this:

retrieveChoices = localStorage.getItem("choices");
choicesObject = JSON.parse(retrieveChoices);
for(var i = 0;i<9 ;i++){

This nex t line is the problem:

    ticked = choicesObject.measure+i;

It just doesn't work and I've tried using quotes and square brackets.

    element = "#m" + i;
    if(ticked==true){
        $(element).prop('checked', true);
    }
    else{
        $(element).prop('checked', false);
    } 
}   

}

I want to loop though the measure properties and restore the checkbox elements.

I'm aware that even my object create is inefficient and I could use a for loop for that but I just don't know how to deal with object properties when it comes to looping because I don't get how you can do it without breaking the object.

At least that works and I can get data into and out of objects that get stored in localStorage, but this really simple issue has me stumped.

PS. Would

choicesObject = localStorage.getItem(JSON.parse("choices"));

be a better shorthand? Just thought this now whilst re-reading my question.

Edit: Thanks everyone. I got 3 correct answers so quickly! Amazing. Thanks so much. This site and its members amaze me every day and have revolutionised my coding!

I'm going to choose the correct answer as the one that also gave me the new shorthand for my parsing, but all of you gave me what i needed to know. I'm going to go see if I can answer some noob questions now!

Upvotes: 0

Views: 74

Answers (2)

Rui
Rui

Reputation: 4886

An object is just like a "dictionary" of values, so you can access a property either by doing myobject.propertyName or myobject["propertyname"]. They are equivalent.

In your case you just have to replace ticked = choicesObject.measure+i; with

ticked = choicesObject["measure"+i];

Also, consider using the var keyword when defining variables, each time you ommit it a new global variable will be created in the window object, that is the case for retrievedChoices and choicesObject. You can confirm this by accessing them via window["choicesObject"] or window.choicesObject or just choicesObject anywhere after that script has run.

Upvotes: 1

user1983983
user1983983

Reputation: 4841

Use

ticked = choicesObject["measure"+i];


EDIT: Your shorthand would not work, use instead:

choicesObject = JSON.parse(localStorage.getItem("choices"));

Upvotes: 1

Related Questions