2013-07-19 72 views
0

我想創建一個新的對象,通過下面的循環,並結合具有類型的對象的值?JavaScript對象文字,結合在同一文字內的對象的值

所以我有這樣的事情:

var data = [{ 
       transID: 123, 
       repId: 86, 
       profit: 117.3, 
       expense: 96.76 }, 
      { 
       transID: 124, 
       repId: 35, 
       profit: 47.39, 
       expense: 15.15 }, 
      { 
       transID: 125, 
       repId: 86, 
       profit: 62.62, 
       expense: 15.17 }] 

,所以我需要同創建如上一個新的陣列,只爲每個代表我想要一個對象,利潤和費用等的總和(所以沒有無論是TRANSID):

  [{ 
       repId: 86, 
       profit: 179.92, 
       expense: 111.93 }, 
      { 
       repId: 35, 
       profit: 47.39, 
       expense: 15.15 }] 

我剛學(去容易,這可能是完全錯誤的),但我已經嘗試了諸如:

var otherData = []; 
    for (var i = 0; i < data.length; i++) { 
     var thisDataObj = data[i]; 
     for (var j = 0; j < data.length; i++) { 
     var thisComparedData = data[j]; // maybe a ridiculous approach ?? 
     if(thisDataObj.repId == thisComparedData.repId) { 
     if(thisDataOjb.transId != thisComparedData.transId) { 
      // struggling with this part if this is event the right approach 
      // the loop is bad because it will recount 
     } 
     } 
    } 

所以沒有顯示我在那個循環中試過的東西,我很確定這只是錯誤的,因爲它會重新計算值,如果我試圖總結,創建對象並推送它。只是不確定什麼是更好的方法?

任何幫助,非常感謝。

回答

3

只是不確定什麼是更好的方法?

是的,你可以使用一個對象作爲查找圖新對象由REPID:

var map = {}; 
for (var i=0; i<data.length; i++) { 
    var obj = data[i], 
     id = obj.repId; 
    if (id in map) { // we know this id already 
     // get the object and sum properties 
     map[id].profit += obj.profit; 
     map[id].expense += obj.expense; 
    } else // create a new one 
     map[id] = { 
      repId: id, 
      profit: obj.profit, 
      expense: obj.profit 
     }; 
} 
/* map: 
{ 
    "35":{"repId":35,"profit":47.39,"expense":47.39}, 
    "86":{"repId":86,"profit":179.92,"expense":132.47} 
} */ 
// put the values from the map in an array 
var otherData = []; 
for (var id in map) 
    otherData.push(map[id]); 
/* otherData: 
[ 
    {"repId":35,"profit":47.39,"expense":47.39}, 
    {"repId":86,"profit":179.92,"expense":132.47} 
] */ 
+0

這真的幫助了我....我學到了新的東西,很酷。謝謝! – Justin

0

我對你試圖達到的確切數學有點不清楚,但我想我可以幫助循環。你不需要兩個循環,只需要一個循環遍歷數據對象的數組。然後,建立自己的新對象的新陣列。像:

var newData = []; 
for (var i=0; i<data.length; i++) { 
    var currentDataObject = data[i]; // don't really need this but just to make it clear 
    newData.push({ 
     repId: currentDataObject.repId, 
     profit: your math here, 
     expense: your math 
    }); 
} 

爲了記錄在案不過,如果你確實需要通過一個對象的屬性進行迭代,你需要使用「爲(VAR的關鍵數據)」語法。

+0

'選擇REPID,SUM(利潤),SUM(費用)FROM數據GROUPBY(REPID)'如果你知道SQL嗎?你的腳本不能做到這一點。 – Bergi

2

排序基於repId您的陣列:

data.sort(function(a,b) { return a.repId - b.repId }); 

現在,你可以通過剛纔循環data,想看看當repId變化:

var prevValue = data[0].repId; // initialize with first data set 
var profit = data[0].profit; 
var expense = data[0].expense; 

for (var i = 1; i < data.length; ++i) { 
    var thisDataObj = data[i]; 
    alert("Start of loop. prevValue = " + prevValue + ", repId = " + thisDataObj.repId); 
    if (thisDataObj.repId == prevValue) { 
     profit += thisDataObj.profit; 
     expense += thisDataObj.expense; 
     alert("profit = " + profit + ", expense = " + expense); 
    } else { 
     alert("prevValue = " + prevValue + ", profit = " + profit + ", expense = " + expense); 
     otherData.push({ 
      repId: prevValue, 
      profit: profit, 
      expense: expense 
     }); 
     prevValue = thisDataObj.repId; 
     profit = thisDataObj.profit; 
     expense = thisDataObj.expense; 
    } 
} 
otherData.push({ // add the last value, since there won't be a change detected for it 
    repId: prevValue, 
    profit: profit, 
    expense: expense 
}); 

如果你不想改變data通過排序排序,首先使用一個副本:

var newArray = data.concat(); 

編輯基於Bergi做出的修正告訴我我的代碼不起作用。這個可以,見the FIDDLE

+0

用'O(n log n)'運行時(不是最優的但比n 2好)的好主意。然而,它不能像現在寫的那樣工作 - 你應該試試你的代碼:-) – Bergi

+0

@Bergi嗯,我對聚會有點晚了,但我修正了一些並且檢查了我的代碼。 (不是非常健壯,但應該足夠了。)謝謝你的鼓勵。 :-) –

+1

謝謝:-)順便說一句,你可以縮短排序'data.sort(函數(a,b){返回a.repId-b。REPID; })' – Bergi

相關問題