1

I am building a website using HTML. In the js file I have an array of various city names.

I want to store in sessionStorage the city name as a key and a number as a value (depending on an if statement). I know I can use for example:

sessionStorage.London= 1;

but what if I don't know the names before hand or I want to write one line in a loop in order to do that ?

To be clearer, What I want is to be able to do something like:

sessionStorage.cityName[i]= 1;

Any solution is welcome

2 Answers 2

1

If you have dynamic object properties, to which you want to assign values, you can do it like sessionStorage[ cityName[i] ] = 1;

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

1 Comment

Glad to know that :)
0

I would use a JSON string in sessionStorage (If I understood your question right):

 // Get the session storage cities object:
 var cities = JSON.parse(sessionStorage.getItem('cities'));

// Set a new city
cities['london'] = 1;

// Save the updated item
 sessionStorage.setItem('cities', JSON.stringify(cities));

Comments

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.