how to create json object in angular js
91,632
Solution 1
As Abdul has pointed out you have a JSON array and you want a JSON object, there you need
var eachProduct =
{
"name": name,
"id": id,
"category":category,
"price":price
};
Now alert(eachProduct.name);
will return name. And I assume by "How to pass the function parameters to the each product" you mean add an attribute to your JSON object. To do this you you would have
eachProduct["someAttribute"]='"value":someValue';
Solution 2
You have declared eachProduct
as an array, you need to use eachProduct[0].name
instead.
Solution 3
You can use forEach to iterate through the array. Please find the jsFiddle working on your example.
Snippet of code:
eachProduct.forEach(function(obj){
alert(obj.name);
});
Author by
sam
Updated on November 05, 2020Comments
-
sam over 3 years
this.addToCart = function(id,name,category,price) { alert(id+"name"+name); var eachProduct = [ { "name": name, "id": id, "category":category, "price":price } ]; alert(eachProduct.name);//I am getting undefine addedProductsList.push(eachProduct); sessionStorage.setItem("addedProductsList", addedProductsList); return "success"; };
How to pass the function parameters to each product?
-
sam about 10 yearshow to push eachProduct array into addedProductList array and how to retrieve it from the storage and get eachProduct name
-
Abdul Fatir about 10 yearsdid you try
addedProductsList.push(eachProduct[0]);