-
Notifications
You must be signed in to change notification settings - Fork 0
Grouping
Aferz edited this page Feb 3, 2016
·
1 revision
Vivaz offers a way to group your results, and as you can expect, is as easy as let the groupBy do its work.
var data = [
{
id: 1,
name: 'Alex',
car: {
brand: 'Hyundai',
model: 'i30'
}
},
{
id: 2,
name: 'Tamara',
car: {
brand: 'Audi',
model: 'A1'
}
},
{
id: 3,
name: 'Celia',
car: {
brand: 'Audi',
model: 'A3'
}
}
];
var result = Vivaz( data ).groupBy( 'car.brand' ).get();
/*
{
'Hyundai': [ { id: 1, name: 'Alex' ... } ],
'Audi': [
{ id: 2, name: 'Tamara' ... },
{ id: 3, name: 'Celia' ... }
]
}
*/
If you want group by multiple fields, you have several ways to do it:
- .groupBy( 'path.to.field1', 'path.to.field2', 'field3' )
- .groupBy( [ 'path.to.field1', 'path.to.field2', 'field3' ] )
var result = Vivaz( data ).groupBy( 'car.brand', 'car.model' ).get();
/*
{
'Hyundai': {
'i30' : [ { id: 1, name: 'Alex' ... } ]
},
'Audi': {
'A1': [ { id: 2, name: 'Tamara' ... } ],
'A3': [ { id: 3, name: 'Celia' ... } ]
}
}
*/
Note: Remember, groupBy object doesn't return an array, and due to that, it returns and object instead a collection.