2016-12-02 99 views
-3

我有對象的數組是這樣的:如何總結對象陣列相同的密鑰值

var original_db = [ 
      {date_diff:"2016/10/31", date_number:1,hangup_cause:"NORMAL_CLEARING"}, 
{date_diff:"2016/10/31", date_number:1,hangup_cause:"NORMAL_CLEARING"}, 
{date_diff:"2016/10/31", date_number:1,hangup_cause:"NO_ANSWER"}, 
{date_diff:"2016/10/31", date_number:1,hangup_cause:"ORIGINATOR-CANCEL"}, 
{date_diff:"2016/10/30", date_number:2,hangup_cause:"NORMAL_CLEARING"}, 
{date_diff:"2016/10/30", date_number:2,hangup_cause:"NO_ANSWER"}, 
{date_diff:"2016/10/30", date_number:2,hangup_cause:"NO_ANSWER"}, 
{date_diff:"2016/10/30", date_number:2,hangup_cause:"NO_ANSWER"}, 
{date_diff:"2016/10/29", date_number:3,hangup_cause:"NORMAL_CLEARING"}, 
{date_diff:"2016/10/29", date_number:3,hangup_cause:"ORIGINATOR-CANCEL"}, 
{date_diff:"2016/10/29", date_number:3,hangup_cause:"ORIGINATOR-CANCEL"}, 
     ]; 

這裏是我期望的結果:

result : 
[ 
{date_diff : "2016/10/31", NORMAL_CLEARING : 2, NO_ANSWER : 1, ORIGINATOR-CANCEL : 1}, 
{date_diff : "2016/10/30", NORMAL_CLEARING : 1, NO_ANSWER : 3}, 
{date_diff : "2016/10/29", NORMAL_CLEARING : 1, ORIGINATOR-CANCEL : 2} 
] 

是否有一個簡單的方法來做到這一點?

+3

是。你試過什麼了? – GAntoine

+0

我試試這段代碼..... var dict = {}; for(i = 0; i user7238915

+0

請回顧[問],並更新您的問題,以包括你嘗試***的[mcve],否則這個問題是[off-topic(#1)](/ help/on-topic )。 – zzzzBov

回答

1

for循環的每次迭代中,檢查「date_number」是否與上一次迭代相同。

如果它不是相同的日期,push()數組中的對象並重置它。

此代碼片段正在工作。

var original_db = [ 
 
    {date_diff:"2016/10/31", date_number:1,hangup_cause:"NORMAL_CLEARING"}, 
 
    {date_diff:"2016/10/31", date_number:1,hangup_cause:"NORMAL_CLEARING"}, 
 
    {date_diff:"2016/10/31", date_number:1,hangup_cause:"NO_ANSWER"}, 
 
    {date_diff:"2016/10/31", date_number:1,hangup_cause:"ORIGINATOR-CANCEL"}, 
 
    {date_diff:"2016/10/30", date_number:2,hangup_cause:"NORMAL_CLEARING"}, 
 
    {date_diff:"2016/10/30", date_number:2,hangup_cause:"NO_ANSWER"}, 
 
    {date_diff:"2016/10/30", date_number:2,hangup_cause:"NO_ANSWER"}, 
 
    {date_diff:"2016/10/30", date_number:2,hangup_cause:"NO_ANSWER"}, 
 
    {date_diff:"2016/10/29", date_number:3,hangup_cause:"NORMAL_CLEARING"}, 
 
    {date_diff:"2016/10/29", date_number:3,hangup_cause:"ORIGINATOR-CANCEL"}, 
 
    {date_diff:"2016/10/29", date_number:3,hangup_cause:"ORIGINATOR-CANCEL"}, 
 
]; 
 
    
 
var resultArr = []; 
 
var resultObj = {}; 
 
var date_number = 1; 
 

 
for (i=0; i<original_db.length; i++) { 
 
    
 
    // If the "date_number" changed, push the object to array. 
 
    if (date_number != original_db[i]["date_number"]){ 
 
     date_number = original_db[i]["date_number"] 
 
     resultArr.push(resultObj); 
 
     resultObj = {}; 
 
    } 
 
    
 
    // Count add each "hangup_cause" and its count 
 
    resultObj["date_diff"] = original_db[i]["date_diff"]; 
 
    var value = original_db[i]["hangup_cause"]; 
 
    var count = (resultObj[value] || 0) + 1; 
 
    resultObj[value] = count; 
 
} 
 

 
// Push the last object at the end of the loop 
 
resultArr.push(resultObj); 
 

 
// Show it in console. 
 
console.log(JSON.stringify(resultArr))

相關問題