I’m using PHP to return a JSON object which looks like this:
[
{"product_line":"Health and beauty","sum":"17014.4"},
{"product_line":"Electronic accessories","sum":"15175.26"},
{"product_line":"Home and lifestyle","sum":"15758.4"},
{"product_line":"Sports and travel","sum":"16839.57"},
{"product_line":"Food and beverages","sum":"15936.37"},
{"product_line":"Fashion accessories","sum":"13296.11"},
{"product_line":"Toys","sum":"150"}
]
Here is the jQuery I’m using in an attempt to set each JSON item to it’s own array:
$.get("process/getTotalSales.php", function (data) {
var obj = JSON.parse(data);
var prodLine = [];
var total = [];
for (var i = 0; i < obj.length; i++) {
prodLine = obj[i].product_line;
total = obj[i].sum;
}
console.log(prodLine);
});
I’d like to get each item into it’s own array to look like this;
var prodLine = ["Health and beauty", "Electronic accessories", // and so on];
Using the above, I am only able to print out “Toys” in the console.
What am I doing wrong and how can I fix it?