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, 2019 in Others by shubham
• 6,910 points
1,912 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, 2019 by sunshine
• 1,260 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, 2019 in Others by Frankie
• 9,810 points
33 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,130 points
60 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,130 points
485 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
96 views
0 votes
0 answers
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

4 days ago in Web Development by anonymous
• 430 points
17 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered 1 day ago in Web Development by Niroj
• 390 points

edited 18 hours ago by Niroj 11 views
+1 vote
1 answer

What are pseudo class in css??

Hey, The state of an element is controlled  by ...READ MORE

answered 1 day ago in Web Development by Niroj
• 390 points

edited 18 hours ago by Niroj 8 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, 2019 in Others by sunshine
• 1,260 points
952 views
0 votes
1 answer

Why and when to use Node.js?

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

answered Jun 14, 2019 in Others by sunshine
• 1,260 points

edited Jun 14, 2019 by sunshine 80 views