Page 1 of 1

How do I loop through a model storage array?

Posted: Sun Nov 16, 2014 3:49 am
by adam griffin

I have saved a database list into a database model in a local storage array using the new builder. How can I loop through each of the object from the list and show the attributes. I am trying to use this but it does not work. It just say undefined.

code
var data = localStorage.getItem("following_Post_Timeline_DB");
for(var i=0; i<data&#46;length;i++){
console&#46;log("Username: "+data&#46;Username+", Post: "+data&#46;Post+"\n");
}

/code


How do I loop through a model storage array?

Posted: Mon Nov 17, 2014 4:53 am
by Yurii Orishchuk

Hi Adam,

There is string type stored in local storage.

So at first you need to convert this JSON string into pure js object.

Here is a code:

precode

var dataString = localStorage&#46;getItem("following_Post_Timeline_DB");
var data = JSON&#46;parse(dataString);

for(var i=0; i<data&#46;length; i++){
console&#46;log("Username: "+data&#46;Username+", Post: "+data&#46;Post+"\n");
}

/code/pre

Regards.


How do I loop through a model storage array?

Posted: Mon Nov 17, 2014 5:46 am
by adam griffin

Thank You


How do I loop through a model storage array?

Posted: Wed Mar 11, 2015 1:09 pm
by Joe Sharples

Hi,

I tried this:
code

var dataString = localStorage&#46;getItem("VenueNightArrayLS");
var data = JSON&#46;parse(dataString);

for(var i=0; i<data&#46;length; i++){

Code: Select all

 if (data[i]&#46;NightName == "Open") 

{
console&#46;log(data&#46;NightName);
}
}

/code

but I get this error in the console:
Image

on this test example there where 3 arrays, one of them contained the 'NightName' "Open".

I get the error 3 times and "Open" is logged to the console once.

So it seems to work but I'm not sure whats causing the error.


How do I loop through a model storage array?

Posted: Wed Mar 11, 2015 1:37 pm
by Joe Sharples

Ignore this...
I had a bit of JS on the mapping that wasnt supposed to be there.

Not receiving this error now I have removed it.

The initial code in this post works great, thanks


How do I loop through a model storage array?

Posted: Wed Mar 11, 2015 1:40 pm
by Alena Prykhodko

Thank you for update.