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
8,249 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
89 views
0 votes
0 answers
0 votes
1 answer

How to create an Icon Button in Flutter?

Hi@akhtar, To create an Icon Button in Flutter, ...READ MORE

answered Jul 24 in Others by MD
• 55,540 points
42 views
0 votes
1 answer

How to create an input field in Flutter?

Hi@akhtar, There are different ways to create an ...READ MORE

answered Aug 9 in Others by MD
• 55,540 points
38 views
0 votes
1 answer

How to store input value into array then localstorage?

Hello @ abhittac, You have create the array everytime the ...READ MORE

answered Jul 24 in Java-Script by Niroj
• 57,700 points
79 views
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

Jan 17 in Web Development by anonymous
• 26,740 points
84 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 Jan 20 in Web Development by Niroj
• 57,700 points

edited Jan 21 by Niroj 122 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
3,826 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 130 views