2017-08-02 18 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

相关问题