0

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}
                                          }
                            },

2 Answers 2

1

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)),
};
Sign up to request clarification or add additional context in comments.

4 Comments

$(obj).prop('id') should just be this.id
@xdazz I never said that you want exactly this ;p
if the each loop doesn't have anything to add, and product_data remains equaling {}, it doesn't show up in the results. is there a way to make it show up but empty?
@seesoe and what would you expect to happen if it doesn't? an Object has length, add a conditional statement to check if it has length, if it doesn't, push some null values into the object like I showed you above.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.