简体   繁体   中英

group by in underscore js

{
     collectionId: 1,
     category: 'a',
     collectionType: 'typea'
 }, {
     collectionId: 1,
     category: 'a',
     collectionType: 'typea'
 }, {
     collectionId: 1,
     category: 'b',
     collectionType: 'typea'
 }, {
     collectionId: 2,
     category: 'b',
     collectionType: 'typeb'
 }, {
     collectionId: 2,
     category: 'b',
     collectionType: 'typeb'
 },

How can i do folllowing use cases :

A) 2 books with same collectionType and same category: Both books should be grouped together and displayed as collection in that category

B) 2 books with same collectionType but different category: Both books should be grouped together and displayed as collection in separate shelf called as "My Collection". ( This shelf is in addition to other categories on bookshelf)

C) 3 books with same collectionType but 2 books have same category and 1 has different category: All 3 books should be grouped together and displayed as collection in separate shelf called as "My Collection". ( This shelf is in addition to other categories on bookshelf)

var LIMIT = 2,
    C_LIMIT = 3;

// A
_(coll)
.chain()
.groupBy(i => i.collectionType.concat(i.category))
.findWhere(i => i.length > LIMIT)
.first(LIMIT)
.value();

// B
_(coll)
.chain()
.groupBy('collectionType')
.mapObject(i => _(i).indexBy('category'))
.values()
.findWhere(i => _(i).keys().length >= LIMIT)
.values()
.first(LIMIT)
.value();

// C
_(coll)
.chain()
.groupBy('collectionType')
.mapObject(function(i) {
    return _(i)
    .chain()
    .groupBy('category')
    .values()
    .map(v => _(v).first(LIMIT))
    .sortBy('length')
    .reverse()
    .flatten()
    .first(C_LIMIT)
    .value()
})
.values()
.findWhere(i => i.length === C_LIMIT)
.value(); 

 var coll = [{ collectionId: 1, category: 'a', collectionType: 'typea' }, { collectionId: 1, category: 'a', collectionType: 'typea' }, { collectionId: 1, category: 'b', collectionType: 'typea' }, { collectionId: 2, category: 'b', collectionType: 'typeb' }, { collectionId: 2, category: 'b', collectionType: 'typeb' }]; var LIMIT = 2, C_LIMIT = 3, el = document.getElementById('result'); var a = _(coll) .chain() .groupBy(i => i.collectionType.concat(i.category)) .findWhere(i => i.length > LIMIT) .first(LIMIT) .value(); var b = _(coll) .chain() .groupBy('collectionType') .mapObject(i => _(i).indexBy('category')) .values() .findWhere(i => _(i).keys().length >= LIMIT) .values() .first(LIMIT) .value(); var c = _(coll) .chain() .groupBy('collectionType') .mapObject(function(i) { return _(i) .chain() .groupBy('category') .values() .map(v => _(v).first(LIMIT)) .sortBy('length') .reverse() .flatten() .first(C_LIMIT) .value() }) .values() .findWhere(i => i.length === C_LIMIT) .value(); el.innerHTML = _([a, b, c]).map(x => JSON.stringify(x)).join('<br><br>');
 <script src="http://underscorejs.org/underscore-min.js"></script> <div id="result"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM