1

我正在尝试遍历发票列表及其各个 LineItem 值,最后有一个 [object Arrays] 的 Ojbect 带有发票编号和每个发票的所有行项目的总值。

var objInvoiceLineItem = function (strInvoiceNo,strValue) {
    this.InvoiceNo= strInvoiceNo;
    this.Value = strValue;
}

//

var objAllInvoices = [];

//

function AddValueTo_objAllInvoices(myInvoice){
    //don't know how to look and see if the Invoice exists? 
     //jQuery.inArray? 
     //for (var i = 0; i < objAllInvoices.length - 1; i++)?

    if exists (myInvoice.InvoiceNo) = false{
     var newObjInvoiceItem= 
          new objInvoiceLineItem(myInvoice.InvoiceNo, myInvoice.Value);
         objAllInvoices.push(newObjInvoiceItem)
    }
    else{
          //need help here please
         var obj = getobject;
         objAllInvoices.obj.Value += myInvoice.Value;
    }  
}

//

var Invoice1A = new objInvoiceLineItem("Invoice1",20);
var Invoice1B = new objInvoiceLineItem("Invoice1",50);
var Invoice2A = new objInvoiceLineItem("Invoice2",30);


AddValueTo_objAllInvoices(Invoice1A);
AddValueTo_objAllInvoices(Invoice1B);
AddValueTo_objAllInvoices(Invoice2A);
4

1 回答 1

2

我认为这样的事情会做你想要的:

function AddValueTo_objAllInvoices(myInvoice)
{
    for (var i = 0; i < objAllInvoices.length; i++)
    {
        if (objAllInvoices[i].InvoiceNo == myInvoice.InvoiceNo)
        {
            // invoice exists, update it and return
            objAllInvoices[i].Value += myInvoice.Value;
            return;
        }
    }

    // if the invoice already existed, we would have returned in the loop
    // so we wouldn't have ever gotten here, so the invoice must not exist.
    // create it now:
    var newObjInvoiceItem = new objInvoiceLineItem(myInvoice.Container, myInvoice.Value);
    objAllInvoices.push(newObjInvoiceItem);
}
于 2012-08-29T15:23:40.157 回答