Reputation: 402
im trying to create a final object with data in it, all of the elements are numbers except for one of them, and that one is to hold more objects. here is what i have so far.
var data = '';
$(".item-element").each(function(e) {
var id = this.id;
var qty = parseInt($("#" + this.id + " .element-qty").text());
});
var sale_data = {
"id" : $('#sales_id').val(),
"cid" : $('#sales_custom').val(),
"status" : $('#sales_status').val(),
"data" : {
"services" : service_data,
"products" : product_data
},
"total" : Number($('#sale-total').text().substr(1)),
};
im trying to get product_data to hold the following type structure from the values of the id and qty in the each loop. the final data entry to look like this based on the each loop.
"data" : {
"services" : service_data,
"products" : {
{id:qty},
{id:qty},
{id:qty}
}
},
Upvotes: 0
Views: 154
Reputation: 50777
I think what you want is something like this
var product_data = {};
$.each($('.item-element'), function(i,obj){
product_data[this.id] = $("#" + this.id + " .element-qty").text();
});
var sale_data = {
"id" : $('#sales_id').val(),
"cid" : $('#sales_custom').val(),
"status" : $('#sales_status').val(),
"data" : {
"services" : service_data,
"products" : product_data
},
"total" : Number($('#sale-total').text().substr(1)),
};
Upvotes: 1
Reputation: 571
It looks like "products" would be more appropriate as an array.
Consider the following.
...
"data" : {
"services" : service_data,
"products" : []
},
...
Based on what you've provided, you could then update your each loop to use something similar to this.
$(".item-element").each(function(e) {
sale_data.data.products.push( { this.id : parseInt($("#" + this.id + " .element-qty").text()) } );
});
Upvotes: 0