1
this.addToCart = function(id,name,category,price) {

        alert(id+"name"+name);
        var eachProduct = [
                {
                    "name": name,
                    "id": id,
                    "category":category,
                    "price":price
                }
            ];

        //$scope.obj = item;
        //alert($scope.obj.name);
        alert("product Name :"+eachProduct[0].name);

        var arrayList = [];
        arraylist.push(eachProduct);

        sessionStorage.setItem("addedProductsList", eachProduct);

        return "success";

and I am retrieving the values arrayList from the sessionStorage

var retrieveArray= sessionStorage.addedProductsList;
alert(retrieveArray.eachProduct[0].name);//getting undefined

I am retrieving in another service how to push each product into arrayList and store it in the session storage.

2
  • Try replacing: alert(retrieveArray.eachProduct[0].name); with alert(retrieveArray[0].name); Commented Jun 9, 2014 at 8:45
  • What is your question? Commented Jun 9, 2014 at 8:49

1 Answer 1

2

Local- and SessionStorage is intented to be used with primitive data types. You need to convert your array to a string before saving it:

sessionStorage.setItem('addedProductsList', JSON.stringify(eachProduct));

and parse it back to an array when retrieving it:

var retrieveArray= JSON.parse(sessionStorage.addedProductsList);
Sign up to request clarification or add additional context in comments.

3 Comments

Local and session storage store string values, and that is the only data type they store.
Demo using AngularJS toJson/fromJson methods: plnkr.co/edit/DIkSfMAoSwfhOOGkE1qh?p=preview
@JLRishe Yep, you're right. My point was that storing a number still works better than storing arrays or objects, even though you get a String and not a Number back.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.