pritu patel
Posts: 0
Joined: Tue Jan 21, 2014 6:24 am

store and retrieve multiple value from local storage.

I am trying to store multiple value in local storage variable..
how to retrive only one from local sorage and how to compare with inputbox value ?????
Image

cityarray == Mumbai,Pune,Delhi this value are store in local storage and How to retrive it and compare with inputbox value with on click..

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

store and retrieve multiple value from local storage.

Hello! In localStorage you can store strings only. If you need to store array or object you can save them as JSON string and parse when you retrieve it.
For example to save array prelocalStorage.setItem("arrayName", JSON.stringify(data));/prewhere data - variable which contains array with data. To read prevar arrayFromLS;
try {
arrayFromLS = JSON.parse(localStorage.getItem("arrayName"));
if ({}.toString.call(arrayFromLS) !== "[object Array]") {
arrayFromLS = [];
}
} catch ( e ) {
arrayFromLS = [];
}/pre

pritu patel
Posts: 0
Joined: Tue Jan 21, 2014 6:24 am

store and retrieve multiple value from local storage.

It's working Maryna..thnk u

pritu patel
Posts: 0
Joined: Tue Jan 21, 2014 6:24 am

store and retrieve multiple value from local storage.

now i want to compare with input box value from array value .how to do it???

pritu patel
Posts: 0
Joined: Tue Jan 21, 2014 6:24 am

store and retrieve multiple value from local storage.

var arr = JSON.parse(localStorage.getItem("cityarray"));
try {
if ({}.toString.call(arr) !== "[object Array]")
{
var x = document.getElementsByName("inputcity").value;
alert(x);
}
else
{
alert(arr[0]['male']);
}
} catch ( e ) {
alert("Parse Error. Some wierd data in localstorage...");
}

It's not working....

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

store and retrieve multiple value from local storage.

Hi Pritu,

What exactly doesn't work? What alert is displayed?

pritu patel
Posts: 0
Joined: Tue Jan 21, 2014 6:24 am

store and retrieve multiple value from local storage.

actually I want to
first store array value in localsotrage as above Maryna told me and i got that solution

second I have input box and in that i enter city name i want to retrive it and check with arrary value that i store in local storage..

for example {mumbai,pune,delhi} is my array
for example input box have pune

then the condition will true and city will find and call another service.
if condition flase then i want to open popup dialog and choose city from that and call service.

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

store and retrieve multiple value from local storage.

Hello!
1) prevar arr = JSON.parse(localStorage.getItem("cityarray")); /pre should be inside try..catch

2) Instead predocument.getElementsByName("inputcity").value/pre use preAppery("componentName").val()/pre3) pre {mumbai,pune,delhi}/pre is not json array. Array should look like this pre["mumbai","pune","delhi"]/pre4) To check if there is x value in arr array use preif (arr.indexOf(x) !== -1) {
//we have this value in array
} else {
//there is no such value in array
}/pre

pritu patel
Posts: 0
Joined: Tue Jan 21, 2014 6:24 am

store and retrieve multiple value from local storage.

thnks Maryna...It's working..

but i tried follow code.
var arr = JSON.parse(localStorage.getItem("CityArray"));
try {
if (arr)
{

Code: Select all

     var city=localStorage.getItem("c"); 

 if (arr.indexOf(city) !== -1) 
      { 

alert(true);
}
else
{
alert(false);
}

Code: Select all

 } 

else
{
alert("array null");
}
} catch ( e ) {
alert(e);
}

Return to “Issues”