数组分两次并计算出现次数

时间:2017-10-19 13:38:55

标签: javascript arrays sorting lodash

我有以下数组:

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' }
];

我正在尝试将它们首先按idOne分组,然后按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,只要它起作用。

3 个答案:

答案 0 :(得分:2)

您可以使用countBy代替groupBy:

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

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




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