fede_rosario
fede_rosario

Reputation: 105

Grouping array by same property value, iterate over that array

I have the following array:

var results = [
  { group: 1,
    department: 2,
    total: 10 },
  { group: 1,
    department: 2,
    total: 25 },
  { group: 1,
    department: 3, 
    total: 15 },
  { group: 1,
    department: 3,
    total: 5 },
  { group: 2,
    department: 4,
    total: 10},
  { group: 2,
    department: 4, 
    total: 50 }
]

I need to arrange the array into a 2D nested array, where the sub arrays are Group 1 and Group 2. The sub arrays should also be parent arrays, whose children are ordered by Department, like so:

var results:[  
   {  
      group1:[  
         {  
            department2:[  
               {  
                  group:1,
                  department:2,
                  total:10
               },
               {  
                  group:1,
                  department:2,
                  total:25
               },
               {  
                  group:1,
                  department:3,
                  total:15
               }
            ]
         },
         {  
            department3:[  
               {  
                  group:1,
                  department:3,
                  total:5
               }
            ]
         }
      ]
   },
   {  
      group2:[  
         {  
            department4:[  
               {  
                  group:2,
                  department:4,
                  total:10
               },
               {  
                  group:2,
                  department:4,
                  total:50
               }
            ]
         }
      ]
   }
]

This is for a react component, where I need to print each one as a row, but add a total after all the department groups are done, and then one after each group, and then a total after everything.

Perhaps I´m over complicating it and there is a better way?

Upvotes: 1

Views: 58

Answers (2)

Jarek Kulikowski
Jarek Kulikowski

Reputation: 1389

var results = [
    { group: 1, department: 2, total: 10 },
    { group: 1, department: 2, total: 25 },
    { group: 1, department: 3, total: 15 },
    { group: 1, department: 3, total: 5 },
    { group: 2, department: 4, total: 10},
    { group: 2, department: 4, total: 50 }
]

var outObj = {};
var out = results.map(function(a) {
    var groupName = 'group' + a.group;
    typeof outObj[groupName] == 'undefined' && ( outObj[groupName] = {});
    var depName = 'department' + a.department;
    typeof outObj[groupName][depName] == 'undefined' && ( outObj[groupName][depName]  = [] );
    outObj[groupName][depName].push(a );
});

console.log( outObj );

Upvotes: 1

akuiper
akuiper

Reputation: 214957

var results = [
  { group: 1,
    department: 2,
    total: 10 },
  { group: 1,
    department: 2,
    total: 25 },
  { group: 1,
    department: 3, 
    total: 15 },
  { group: 1,
    department: 3,
    total: 5 },
  { group: 2,
    department: 4,
    total: 10},
  { group: 2,
    department: 4, 
    total: 50 }
]

var nested_result = {}

results.forEach(obj => {
  if(nested_result[obj.group] === undefined) {
    nested_result[obj.group] = {};
  }
  if(nested_result[obj.group][obj.department] === undefined) {
    nested_result[obj.group][obj.department] = [];
  }
  nested_result[obj.group][obj.department].push(obj);
})

console.log(nested_result);

Upvotes: 1

Related Questions