2017-10-19 1 views
0

Ich habe das folgende Array:Array Gruppe von zweimal und zählen Vorkommnissen

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

ich sie zu einer Gruppe versuchen, zuerst von Idone, dann durch idTwo und zählen, wie viele von ihnen gefunden werden, und dann sortieren sie:

Im Moment habe ich die folgende Methode:

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(); 

Aber ich möchte folgendes Array oder Objekt zurückgeben, nicht sicher, was man am besten wäre:

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

Es muss nicht mit Unterstrich oder Lodash sein, einfach was auch immer es funktioniert.

+0

stammt JS Lösung erlaubt? – RomanPerekhrest

+0

Ich implementiere das in einer Reagieren-App, also was auch immer wirklich funktioniert ... –

+0

möchten Sie nicht ein Objekt mit einer Struktur wie: {[idOne]: {[idTwo]: {Objekt}}}? im Wesentlichen eine Karte von Karten zu den Objekten? – bryan60

Antwort

2

Sie countBy statt groupBy verwenden:

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

Ecamscript5 alternative Lösung (basierend auf Array.reduce() Funktion):

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

Sie könnten ein Array für die Schlüssel-Gruppe bei der Verwendung für die Hash-Tabelle zu zählen.

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);