sam
sam

Reputation: 611

how to push array into another array in angularjs

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.

Upvotes: 1

Views: 2690

Answers (1)

Johan
Johan

Reputation: 35194

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);

Upvotes: 2

Related Questions