2012-11-01 20 views
0

我想在銷售訂單中對相同名稱的項目進行分組,併合並報表中的價格金額。在javascript中分組項目(netsuite)

例如, 銷售訂單

Item A 100usd 
Item A 100usd 
Item A 100usd 
Item A 100usd 

在我要總結的所有項目和展示項目A的總價格作爲一個行報告: 項目A 400USD

我知道我應該使用循環和數組做到這一點,但它似乎並沒有工作。

//scan through all lines 
for(i=1;...){ 
item[i]=getitemforline(i); 

itemprice[i]=getitempriceforline(i); 

} 
//check current line one by one for any duplicates, and sum up itemprice if there is 
for(k=1;...){ 
     for(i=1;i<k;i++){ 
     currentitem[k] = getitemforcurrentline(k); 
     currentitemprice[k] = getitempriceforcurrentline(k); 
     if(currentitem[k] == item[i]){ 
     itemprice[i] = itemprice[i] + currentitemprice[k]; 
     } 
     } 
print(itemw[i]+itemprice[i]); 
} 
+0

這是非常模糊 - 目前數據的結構是什麼? getitemforline()和其他函數返回什麼? – nnnnnn

+0

數據結構採用NetSuite數據庫表格格式。 Getlineforitem(i,price)返回特定行的值。 – user1033038

+0

函數名稱只是僞代碼,並不代表確切的函數名稱。 – user1033038

回答

0

您的for循環不正確。

爲(I = 1;我< K表;我++){

您需要爲(I = 1;我< = K;我++){

0
Use below method for above use case: 

var getLineItemAggregateResult = function(salesOrderId) { 
    if(!salesOrderId) { 
     return; 
    } 

    var salesOrder = nlapiLoadRecord('salesorder', salesOrderId); 
    var lineItemCount = salesOrder.getLineItemCount('item'); 

    /*----------------------------------------------------------------------------------------------------------------------- 
    "lineItem_TotalValue" this is an object, it will contain lineitem as a key and total amount as a value 
    e.g.: {item1 : 400, 
      item2 : 500} 
      As this will a JSON object so you can use this to display your final result. 
    -------------------------------------------------------------------------------------------------------------------------*/ 
    var lineItem_TotalValue = {}; 
    for(var i = 1; i <= lineItemCount; i++) { 
     if (!lineItem_TotalValue[salesOrder.getLineItemValue('item', 'item', i)]) { 
      lineItem_TotalValue[salesOrder.getLineItemValue('item', 'item', i)] = parseInt(salesOrder.getLineItemValue('item', 'amount', i), 10); 
     } 
     else { 
      lineItem_TotalValue[salesOrder.getLineItemValue('item', 'item', i)] += parseInt(salesOrder.getLineItemValue('item', 'amount', i), 10); 
     } 
    } 

    return lineItem_TotalValue; 
}