How to store an array in localstorage?

0 votes

If I don't use localStorage:

var employees=new Array(); 
employees[0]=prompt("New member name?");

This works. However, when I store the variable in localStorage it is not working, the codes I have tried are:

var localStorage[employees] = new Array();
localStorage.employees[0] = prompt("New member name?");

Where am I going wrong?

Jul 1 in Others by shubham
• 6,870 points
25 views

1 answer to this question.

0 votes

Localstorage only supports Strings. So you can use JSON.stringify() as the data which is sent to/from a server is JSON, you can convert it into a string with the help of JSON.stringify().

var employees = [];
employees[0] = prompt("New name?");
localStorage.setItem("employees", JSON.stringify(employees));
var storedNames = JSON.parse(localStorage.getItem("employees"));

answered Jul 1 by sunshine
• 1,200 points

Related Questions In Others

0 votes
1 answer

How can you create an Array in JavaScript?

You can define arrays using the array literal as ...READ MORE

answered Mar 6 in Others by Frankie
• 9,810 points
9 views
0 votes
1 answer

Reducing sequences in an array of strings

I've written a C# app to solves ...READ MORE

answered Nov 2, 2018 in Others by DataKing99
• 8,100 points
40 views
0 votes
1 answer

How to check if array is multidimensional or not?

Since the 'second dimension' could be just ...READ MORE

answered Nov 5, 2018 in Others by DataKing99
• 8,100 points
125 views
0 votes
1 answer

How to get the URL of the current tab in Google Chrome?

There are different ways of doing this:- You ...READ MORE

answered Dec 21, 2018 in Others by Nabarupa
61 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,840 points
77 views
0 votes
1 answer

Presenting docket dtates inside html page by javascript

Use the Docker Engine Api:Docker Engine API ...READ MORE

answered Jun 20, 2018 in Docker by DareDev
• 6,810 points
23 views
0 votes
1 answer

Migrating proxy npm repo in nexus 3

I don't think you can achieve this ...READ MORE

answered Jun 22, 2018 in DevOps Tools by DareDev
• 6,810 points
168 views
0 votes
2 answers

How does random shuffling of an array

Here is a simple way using an ArrayList: List<Integer> ...READ MORE

answered Nov 2, 2018 in Java by Sushmita
• 6,840 points
23 views
0 votes
1 answer

Why do we use Body-parser in Node.js?

For understanding this first you need to ...READ MORE

answered May 20 in Others by sunshine
• 1,200 points
29 views
0 votes
1 answer

Why and when to use Node.js?

The major reason why they are going ...READ MORE

answered Jun 14 in Others by sunshine
• 1,200 points

edited Jun 14 by sunshine 22 views