Page 1 of 2

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

Posted: Fri Feb 27, 2015 1:57 am
by sebastian

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


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

Posted: Fri Feb 27, 2015 2:02 am
by sebastian

Would it be something like?

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

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


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

Posted: Fri Feb 27, 2015 6:32 am
by sebastian

Never mind my question I think I figure it out.


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

Posted: Fri Feb 27, 2015 6:57 am
by Illya Stepanov

Hi Sebastian -

Thank you for update!


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

Posted: Fri Feb 27, 2015 7:05 am
by sebastian

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.


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

Posted: Fri Feb 27, 2015 7:16 am
by sebastian

I might have figured it out again. Thanks!


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

Posted: Mon Mar 09, 2015 6:22 pm
by sebastian

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.


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

Posted: Mon Mar 09, 2015 7:57 pm
by sebastian

Is there any solutions around this?


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

Posted: Mon Mar 09, 2015 9:21 pm
by Egor Kotov6832188

Hello Sebastian,
Try to check length in the if statement


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

Posted: Mon Mar 09, 2015 11:32 pm
by sebastian

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