通过总结相同的元素进行JavaScript数组排序

时间:2017-08-02 02:38:46

标签: javascript arrays sorting

尝试在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 个答案:

答案 0 :(得分:4)

所以我会这样做:

  1. 根据hashmap属性将数组分组为branchName - 计算总数以及此值。

  2. hashmap取出数组并对其进行排序

  3. 见下面的演示:

    &#13;
    &#13;
    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);
    &#13;
    .as-console-wrapper{top:0;max-height:100%!important;}
    &#13;
    &#13;
    &#13;

答案 1 :(得分: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);

答案 2 :(得分:0)

看起来你想要做两件事:按branchName排序,然后为每个branchName输出一个值(重复结果/ group-by branchName)。

有几个潜在的问题。 1)上面显示的示例输出未按branchName排序,即使您声明a)您希望它按branchName排序,并且b)这将是一个示例。其次,输出并不完全是确定性的 - 特别是它似乎只输出第一个匹配的记录形式branchName,因此总属性的值 - 在同一branchName的记录之间变化 - 是显示的。所以...假设你A)想要对结果进行排序,并且B)不关心&#34;总和&#34;的价值。属性,这可以通过以下方式轻松完成:

I)对数组进行排序。有关示例,请参阅https://gist.github.com/umidjons/9614157:只需编写一个比较branchName值的比较函数。和, II)遍历结果,只要在branchName从前一个值改变时输出第一个记录。