sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

How to create object arrays and save them into a local storage using javascript?

Image

I'm trying to create simple shopping cart object array and save it to local storage. How would I write the script out to save this items into an object array.

Lets say the person filled there shopping car with a [Name: "pencil" Price 1.00, Name: "notebook" Price 1.50, and Name: "backpack" Price 25.00].

How would I write the javascript that so those values would be saved in local storage as items[0], items[1], items[2]?

Eventually I'm going to save items[1,2,3] into a single array cell in a database. The array size will be dynamic it might be more then 3 items.

Best regards,
Sebastian

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

How to create object arrays and save them into a local storage using javascript?

Would it be something like?

var item0 = {
Name: "Pencil",
Price: 1.00
};

localStorage.setItem("items[0]", item0);

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

How to create object arrays and save them into a local storage using javascript?

Never mind my question I think I figure it out.

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

How to create object arrays and save them into a local storage using javascript?

Hi Sebastian -

Thank you for update!

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

How to create object arrays and save them into a local storage using javascript?

I do have one question. How to map out array properties from a database? I've seemed to figure out how to create objects and arrays and saved them to a database. I just can't figure out how to list them with their properties.

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

How to create object arrays and save them into a local storage using javascript?

I might have figured it out again. Thanks!

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

How to create object arrays and save them into a local storage using javascript?

I'm having trouble. When I want to create a new array using an if statement based on an array length. If the array length is 0 then the computer reads it as a null value and stops the program.

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

How to create object arrays and save them into a local storage using javascript?

Is there any solutions around this?

Egor Kotov6832188
Posts: 0
Joined: Wed Nov 19, 2014 5:15 pm

How to create object arrays and save them into a local storage using javascript?

Hello Sebastian,
Try to check length in the if statement

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

How to create object arrays and save them into a local storage using javascript?

I figured it out a little before you messaged me. What I did was use an if statement to see if my LSV had a null value and then an else if the LSV wasn't a null value and then I used the LSV.length for my loop

Return to “Issues”