2017-10-19 49 views
0

我有以下陣列:陣列組和計數OCCURENCES

var array = [ 
    { idOne: 1, oneText: 'one', idTwo: 2, twoText: 'two' }, 
    { idOne: 1, oneText: 'one', idTwo: 2, twoText: 'two' }, 
    { idOne: 12, oneText: 'twelve', idTwo: 23, twoText: 'twentythree' }, 
    { idOne: 12, oneText: 'twelve', idTwo: 23, twoText: 'twentythree' }, 
    { idOne: 12, oneText: 'twelve', idTwo: 25, twoText: 'twentyfive' }, 
    { idOne: 16, oneText: 'sixteen', idTwo: 56, twoText: 'fiftysix' }, 
    { idOne: 16, oneText: 'sixteen', idTwo: 56, twoText: 'fiftysix' }, 
    { idOne: 16, oneText: 'sixteen', idTwo: 56, twoText: 'fiftysix' }, 
    { idOne: 16, oneText: 'sixteen', idTwo: 50, twoText: 'fifty' }, 
    { idOne: 16, oneText: 'sixteen', idTwo: 50, twoText: 'fifty' }, 
    { idOne: 18, oneText: 'eighteen', idTwo: 90, twoText: 'ninety' } 
]; 

我想他們組首先吡咯烷酮,然後通過idTwo,並計算有多少人被發現,然後排序其中:

目前,我有以下方法:

var result = _.chain(array) 
    .groupBy("idOne") 
    .map(function(idOne, idOneKey) { 
     return _.chain(idOne) 
      .groupBy("idTwo") 
      .map(function(idTwo, idTwoKey) { 
       return {     
        id: idOneKey + '-' + idTwoKey 
       } 
      }) 
      .value(); 
    }) 
    .value(); 

但我想返回下面的數組或對象,不知道哪一個是最好的:

result = { 
    'one-two': 2, 
    'twelve-twentythree': 2, 
    'twelve-twentyfive': 1, 
    'sixteen-fiftysix': 3 
    ... 
} 

它不必與下劃線或lodash,只要它工作。

+0

是本地JS的解決方案允許嗎? – RomanPerekhrest

+0

我正在實施這個反應的應用程序,所以無論真的工作... –

+0

wouldnt你想要一個像這樣的結構的對象:{[idOne]:{[idTwo]:{object}}}?基本上是一個映射到對象的地圖? – bryan60

回答

2

你可以使用countBy而不是GROUPBY:

let result = _.countBy(array, item => item.oneText + '-' + item.twoText); 
1

Ecamscript5替代解決方案(基於Array.reduce()功能):

var arr = [ 
 
    { idOne: 1, oneText: 'one', idTwo: 2, twoText: 'two' }, { idOne: 1, oneText: 'one', idTwo: 2, twoText: 'two' }, 
 
    { idOne: 12, oneText: 'twelve', idTwo: 23, twoText: 'twentythree' }, { idOne: 12, oneText: 'twelve', idTwo: 23, twoText: 'twentythree' }, 
 
    { idOne: 12, oneText: 'twelve', idTwo: 25, twoText: 'twentyfive' }, { idOne: 16, oneText: 'sixteen', idTwo: 56, twoText: 'fiftysix' }, 
 
    { idOne: 16, oneText: 'sixteen', idTwo: 56, twoText: 'fiftysix' }, { idOne: 16, oneText: 'sixteen', idTwo: 56, twoText: 'fiftysix' }, 
 
    { idOne: 16, oneText: 'sixteen', idTwo: 50, twoText: 'fifty' }, { idOne: 16, oneText: 'sixteen', idTwo: 50, twoText: 'fifty' }, 
 
    { idOne: 18, oneText: 'eighteen', idTwo: 90, twoText: 'ninety' } 
 
], 
 
result = arr.reduce(function(r, o){ 
 
    var k = o.oneText +'-'+ o.twoText; 
 
    r[k] = (r[k])? (r[k]+1) : 1; 
 
    return r; 
 
}, {}); 
 

 
console.log(result);

1

你可以對哈希表來算使用的密鑰的陣列組。

var array = [{ idOne: 1, oneText: 'one', idTwo: 2, twoText: 'two' }, { idOne: 1, oneText: 'one', idTwo: 2, twoText: 'two' }, { idOne: 12, oneText: 'twelve', idTwo: 23, twoText: 'twentythree' }, { idOne: 12, oneText: 'twelve', idTwo: 23, twoText: 'twentythree' }, { idOne: 12, oneText: 'twelve', idTwo: 25, twoText: 'twentyfive' }, { idOne: 16, oneText: 'sixteen', idTwo: 56, twoText: 'fiftysix' }, { idOne: 16, oneText: 'sixteen', idTwo: 56, twoText: 'fiftysix' }, { idOne: 16, oneText: 'sixteen', idTwo: 56, twoText: 'fiftysix' }, { idOne: 16, oneText: 'sixteen', idTwo: 50, twoText: 'fifty' }, { idOne: 16, oneText: 'sixteen', idTwo: 50, twoText: 'fifty' }, { idOne: 18, oneText: 'eighteen', idTwo: 90, twoText: 'ninety' }], 
 
    result = {}; 
 

 
array.forEach(function (o) { 
 
    var key = ['oneText', 'twoText'].map(k => o[k]).join('-'); 
 
    result[key] = (result[key] || 0) + 1; 
 
}); 
 

 
console.log(result);