我有以下陣列:陣列組和計數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,只要它工作。
是本地JS的解決方案允許嗎? – RomanPerekhrest
我正在實施這個反應的應用程序,所以無論真的工作... –
wouldnt你想要一個像這樣的結構的對象:{[idOne]:{[idTwo]:{object}}}?基本上是一個映射到對象的地圖? – bryan60