1

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?

2
  • Your expected API input is not valid json Commented Jul 20, 2017 at 19:29
  • You could use an object instead. Iterate over your array and shove the data in where group# is the key, and department and total are stored in arrays as values. Your current desired structure is redundant. Commented Jul 20, 2017 at 19:29

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect!! Thanks for the help, I was banging my head with this and could not solve it!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.