1

請求書のリストとその個々の LineItem 値をループしようとしています。最終的には、請求書番号と請求書ごとのすべての明細項目の合計値を持つ [オブジェクト配列] のオブジェクトがあります。

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 に答える