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
• 7,340 points
22,368 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,300 points

Related Questions In Others

0 votes
1 answer

How to multi level sort an array in excel, using formulas? I am aware about the way, using SORT button on DATA tab

Use SORTBY, e.g. =SORTBY(A2:B5,A2:A5,1,B2:B5,1) Or simply&nb ...READ MORE

answered Jan 22, 2023 in Others by narikkadan
• 63,420 points
281 views
0 votes
1 answer

How to make an error flagging array in VBA and translate all array elements as a string message?

In my opinion, using an array in ...READ MORE

answered Mar 17, 2023 in Others by Kithuzzz
• 38,010 points
322 views
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 7, 2019 in Others by Frankie
• 9,830 points
450 views
0 votes
0 answers

How to open a new tab or a new window while writing an exam in AI - Onlime Remote Proctored

How to open a new tab or ...READ MORE

Jul 20, 2020 in Others by vimalkamal
• 120 points
3,287 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, 2020 in Java-Script by Niroj
• 82,880 points
8,607 views
0 votes
1 answer
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, 2020 in Web Development by anonymous
• 37,510 points
740 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, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 780 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,300 points
7,645 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,300 points

edited Jun 14, 2019 by sunshine 739 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP