Skip to content

Instantly share code, notes, and snippets.

@thorizer
Last active April 28, 2020 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thorizer/a6abb470dbdada35bf879266d94883e7 to your computer and use it in GitHub Desktop.
Save thorizer/a6abb470dbdada35bf879266d94883e7 to your computer and use it in GitHub Desktop.
Group objects by multiple properties including nested properties and sump up their values
//Input :************************************************************************************************************************************
[
{
"commissioningDate": "2019-09-27",
"_product": {
"_id": "aaa",
"name": "Installation"
},
"zones": [
{
"_zone": {
"_id": "KK",
"name": "Zone kk"
},
"category": "category1",
"zone_quantity": 6
},
{
"_zone": {
"_id": "BB",
"name": "Zone BB"
},
"category": "category1",
"zone_quantity": 3
}
],
"product_quantity": 3
},
{
"commissioningDate": "2019-09-27",
"_product": {
"_id": "aaa",
"name": "Installation"
},
"zones": [
{
"_zone": {
"_id": "KK",
"name": "Zone kk"
},
"category": "category2",
"zone_quantity": 3
}
],
"product_quantity": 2
},
{
"commissioningDate": "2019-09-27",
"_product": {
"_id": "aaa",
"name": "Installation"
},
"zones": [
{
"_zone": {
"_id": "KK",
"name": "Zone kk"
},
"category": "category2",
"zone_quantity": 4
}
],
"product_quantity": 5
},
{
"commissioningDate": "2019-09-27",
"_product": {
"_id": "aaa",
"name": "Installation"
},
"zones": [
{
"_zone": {
"_id": "CC",
"name": "Zone cc"
},
"category": "category2",
"zone_quantity": 6
}
],
"product_quantity": 1
},
{
"commissioningDate": "2019-09-27",
"_product": {
"_id": "bbbb",
"name": "Installation"
},
"zones": [
{
"_zone": {
"_id": "CC",
"name": "Zone cc"
},
"category": "category2",
"zone_quantity": 8
}
],
"product_quantity": 2
},
{
"commissioningDate": "2019-09-26",
"_product": {
"_id": "bbbb",
"name": "Installation"
},
"zones": [
{
"_zone": {
"_id": "CC",
"name": "Zone cc"
},
"category": "category2",
"zone_quantity": 8
}
],
"product_quantity": 2
}
]
//output ************************************************************************************************************************************
[
{
"commissioningDate": "2019-09-27",
"_product": {
"_id": "aaa",
"name": "Installation"
},
"zones": [
{
"_zone": {
"_id": "KK",
"name": "Zone kk"
},
"category": "category1",
"zone_quantity": 6
},{
"_zone": {
"_id": "KK",
"name": "Zone kk"
},
"category": "category2",
"zone_quantity": 7
},{
"_zone": {
"_id": "CC",
"name": "Zone cc"
},
"category": "category2",
"zone_quantity": 6
},{
"_zone": {
"_id": "BB",
"name": "Zone BB"
},
"category": "category1",
"zone_quantity": 3
}
],
"product_quantity": 11
},
{
"commissioningDate": "2019-09-27",
"_product": {
"_id": "bbbb",
"name": "Installation"
},
"zones": [
{
"_zone": {
"_id": "CC",
"name": "Zone cc"
},
"category": "category2",
"zone_quantity": 8
}
],
"product_quantity": 2
},
{
"commissioningDate": "2019-09-26",
"_product": {
"_id": "bbbb",
"name": "Installation"
},
"zones": [
{
"_zone": {
"_id": "CC",
"name": "Zone cc"
},
"category": "category2",
"zone_quantity": 8
}
],
"product_quantity": 2
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment