Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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].

share|improve this answer
    
But objects are unordered. I imagine we want this report to be in a particular order, no? –  Raphael Serota Oct 13 '14 at 15:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.