adam griffin
Posts: 0
Joined: Tue May 21, 2013 8:44 pm

How do I loop through a model storage array?

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

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

How do I loop through a model storage array?

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.

Joe Sharples
Posts: 0
Joined: Mon Aug 18, 2014 1:31 pm

How do I loop through a model storage array?

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.

Joe Sharples
Posts: 0
Joined: Mon Aug 18, 2014 1:31 pm

How do I loop through a model storage array?

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

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

How do I loop through a model storage array?

Thank you for update.

Return to “Issues”