2017-08-02 47 views
3

當嘗試對JavaScript中的數組項進行排序和分組時,我遇到了一些麻煩。這裏是採樣輸入:JavaScript數組排序通過總結相同的元素

var arr = [ 
{merchantName: '', branchName: 'e', branchAddress: '', total: 10.5}, 
]; 

,我想實現的輸出:

var arr = [ 
{merchantName: '', branchName: '', branchAddress: '', total: 10.5}, 
]; 

我想把它由BRANCHNAME排序,例如總結總爲同一BRANCHNAME然後在同時綁定所有其他屬性,如MERCHANTNAME和branchAddress與它在一起,這樣我可以訪問他們喜歡的:

for(var i = 0; i < arr.length; i++){ 
      console.log(arr[i].merchantName + ' ' + arr[i].branchName + ' ' + arr[i].branchAddress + ' ' + arr[i].total); 
     } 

其實我對如何甚至開始它不知道。任何想法如何實現它?

感謝先進!

回答

3

因此,這裏是我會怎麼做:

  1. 組陣列成hashmap基礎上,branchName財產 - 計算總伴隨於此。

  2. hashmap取出數組和對它們進行排序

參見下面演示:使用

var arr = [ 
 
{merchantName: 'Giant', branchName: 'Giant Marine', branchAddress: 'Terrace 56 Branch Blk 56 Marine Terrace #01-259/261 Singapore 440056', total: 10.5}, 
 
{merchantName: 'Ntuc', branchName: 'Ntuc Zhongshan Mall Branch', branchAddress: ' Zhongshan Mall Balestier #02-01, 20 Ah Hood Road, 329984', total: 12.149999999999999}, 
 
{merchantName: 'Giant', branchName: 'Giant Kim Keat 260 Branch', branchAddress: ' Blk 260 Kim Keat Avenue #01-01 Singapore 310260', total: 5.1}, 
 
{merchantName: 'Ntuc', branchName: 'Ntuc Scotts Square Branch', branchAddress: ' Scotts Square #B1-03 To 07 & #B1-10, 6 Scotts Road, 228209', total: 4}, 
 
{merchantName: 'Ntuc', branchName: 'Ntuc Zhongshan Mall Branch', branchAddress: ' Zhongshan Mall Balestier #02-01, 20 Ah Hood Road, 329984', total: 4}, 
 
{merchantName: 'Ntuc', branchName: 'Ntuc Zhongshan Mall Branch', branchAddress: ' Zhongshan Mall Balestier #02-01, 20 Ah Hood Road, 329984', total: 8} 
 
]; 
 

 
// create a hashmap 
 
var hash = arr.reduce(function(p,c){ 
 
    if(!p[c.branchName]) 
 
    p[c.branchName] = c; 
 
    else 
 
    p[c.branchName].total += c.total; 
 
    return p; 
 
}, Object.create(null)) 
 

 
// now extract the result and sort them 
 
var result = Object.keys(hash).map(function(e){ 
 
    return hash[e]; 
 
}).sort(function(a,b){ 
 
    return a.branchName - b.branchName; 
 
}); 
 

 
console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

+0

非常感謝!但是我怎樣才能提取出屬性呢?我試過console.log(result.branchName),但它返回undefined – hyperfkcb

+0

那麼你應該通過'result'循環並執行類似'result [i] .branchName'的操作... – kukkuz

+0

我明白了。非常感謝!但只是爲了雙倍確認,因爲我試圖理解代碼,當你創建哈希映射時,你在哪裏引用了P&C參數?因爲根據我的理解,您正在嘗試檢查branchName是否存在,然後總計總計。如果沒有創建一個新的對象,我說得對嗎? – hyperfkcb

0

看起來你想要做2件事情:按照branchName排序,然後爲每個branchName輸出一個值(去除重複結果/ group-by branchName)。

有幾個潛在的問題。 1)上面顯示的示例輸出沒有按branchName排序,即使您聲明瞭a)您希望它按branchName排序,並且b)這將是一個示例。其次,輸出並不是完全確定性的 - 特別是它似乎只輸出了第一個匹配的記錄格式branchName,因此總屬性的值(在同一個分支名稱的記錄中變化)就是顯示的內容。所以......假設你A)要求結果排序,B)不關心「總」屬性的值,這可以通過以下方式輕鬆完成:

I)對數組進行排序。舉例來說,請參見https://gist.github.com/umidjons/9614157:只需編寫一個比較分支名稱值的比較函數即可。並且, II)循環遍歷結果,只要branchName從前一個值更改時輸出第一條記錄。

2

溶液reduce()

var arr = [{ 
 
    merchantName: 'Giant', 
 
    branchName: 'Giant Marine', 
 
    branchAddress: 'Terrace 56 Branch Blk 56 Marine Terrace #01-259/261 Singapore 440056', 
 
    total: 10.5 
 
    }, 
 
    { 
 
    merchantName: 'Ntuc', 
 
    branchName: 'Ntuc Zhongshan Mall Branch', 
 
    branchAddress: ' Zhongshan Mall Balestier #02-01, 20 Ah Hood Road, 329984', 
 
    total: 12.149999999999999 
 
    }, 
 
    { 
 
    merchantName: 'Giant', 
 
    branchName: 'Giant Kim Keat 260 Branch', 
 
    branchAddress: ' Blk 260 Kim Keat Avenue #01-01 Singapore 310260', 
 
    total: 5.1 
 
    }, 
 
    { 
 
    merchantName: 'Ntuc', 
 
    branchName: 'Ntuc Scotts Square Branch', 
 
    branchAddress: ' Scotts Square #B1-03 To 07 & #B1-10, 6 Scotts Road, 228209', 
 
    total: 4 
 
    }, 
 
    { 
 
    merchantName: 'Ntuc', 
 
    branchName: 'Ntuc Zhongshan Mall Branch', 
 
    branchAddress: ' Zhongshan Mall Balestier #02-01, 20 Ah Hood Road, 329984', 
 
    total: 4 
 
    }, 
 
    { 
 
    merchantName: 'Ntuc', 
 
    branchName: 'Ntuc Zhongshan Mall Branch', 
 
    branchAddress: ' Zhongshan Mall Balestier #02-01, 20 Ah Hood Road, 329984', 
 
    total: 8 
 
    } 
 
]; 
 

 
var newArr = arr.reduce(function(items, item) { 
 

 
    var existing = items.find(function(i) { 
 
    return i.branchName === item.branchName; 
 
    }); 
 
    
 
    if (existing) { 
 
    existing.total += item.total; 
 
    } else { 
 
    items.push(item); 
 
    } 
 
    
 
    return items; 
 
}, []); 
 

 
console.log(newArr);

+0

非常感謝!您的解決方案也可以工 – hyperfkcb

相關問題