0

I'm trying to create a summary report for a order based on products in the order

However my summary array is always empty.

var summary = [];

_.each(this.products, function(product,counter) {
    var invoice = {};

    invoice.total = 0;
    invoice.dealer_discount = 0;
    invoice.program_discount = 0;
    invoice.adjusted_total = 0;

    _.each(product.materials, function(material) {
        _.each(material.variants, function(variant) {
            var difference = 0;

            invoice.total = parseFloat(invoice.total + variant.price.msrp);

            if(variant.price.discount_type === 'dealer') {
                difference = parseFloat(variant.price.msrp - variant.price.discount_price);

                invoice.dealer_discount = parseFloat(invoice.dealer_discount + difference);
            } else {
                difference = parseFloat(variant.price.msrp - variant.price.discount_price);

                invoice.program_discount = parseFloat(invoice.program_discount + difference);
            }   
        });
    });

    // This never seems to get populated?   
    // If I set the array key to counter like summary[counter] it works fine but I need:    
    summary[product.fulfilled_by] = invoice;
});

It is probably something simple that I'm doing wrong.

Any help is appreicated.

1 Answer 1

1

Just changing the first line will solve your problem

var summary = {};  // Object

An Object store items in key : value fashion, while an Array will just contain value which can be accessed by an index which is numeric and hence worked when you put summary[counter].

Sign up to request clarification or add additional context in comments.

1 Comment

But objects are unordered. I imagine we want this report to be in a particular order, no?

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.