Jael Saavedra
Jael Saavedra

Reputation: 138

Can I do multiple group by's with underscore.js?

The problem is that I have a json without parenting. So I need to create the agroupations manually;

Here's my json:

"data":[
    {"AREA":"A/M COMMON","DIVISION":"B","LOCATION":"FBC"},
    {"AREA":"A/M MAINTENANCE","DIVISION":"B","LOCATION":"FBC"},
    {"AREA":"A/M PRODUCTION LCD DIVISION","DIV":"B","LOCATION":"SONY"},
    {"AREA":"AM VIZIO MAINTENANCE","DIVISION":"B","LOCATION":"FBC"},
    {"AREA":"AM VIZIO PRODUCTION","DIVISION":"B","LOCATION":"VIZIO"},
    {"AREA":"BUSINESS PLANNING","DIVISION":"B","LOCATION":"FBC"},
    {"AREA":"COMARKET F/A PRODUCTION DIRECT","DIVISION":"B","LOCATION":"FBC"},
    {"AREA":"CONTROLLERS","DIVISION":"B","LOCATION":"FBC"},
    ]

I'd like to group my data in 3 arrays, first by LOCATION, then DIVISION and last AREA

I tried using underscore.js

    var groupedData = _.groupBy(data, function (d) { return d.LOCATION});

So I want to group by hierarchy, but this method only returns 1 agrupation and I need two.

Is there a way I can do this? I'm so lost.

https://jsfiddle.net/jaelsvd/dwc5mdyL/

Upvotes: 0

Views: 2028

Answers (2)

Ananda
Ananda

Reputation: 918

Underscore has a simpler way to get this running @Jael Saavedra 3rd line in your data array has an error Div should be DIVISION

Have updated your jsfiddle with the solution here https://jsfiddle.net/dwc5mdyL/3/

 var groupedData = _.groupBy(data, function(d) {
return d.LOCATION + "-" + d.DIVISION+ "-" + d.AREA;
});

original sources coderwall google cache

Upvotes: 1

Gruff Bunny
Gruff Bunny

Reputation: 27976

I think this should do what you want:

let result = _.chain(data)
    .groupBy('AREA')
    .mapObject(area => _.groupBy(area, 'DIVISION'))
    .mapObject(area => _.mapObject(area, div => _.groupBy(div, 'LOCATION')))
    .value();

First it groups by AREA giving an object like:

{
    'AM/COMMON': [ /* AM/COMMON objects */ ],
    'A/M MAINTENANCE': [ /* A/M MAINTENANCE */ ],
    etc
}

Then it maps across each object to group by DIVISION'.

Finally it maps across each division object within each area to group by LOCATION.

	let data = [
    {"AREA":"A/M COMMON","DIVISION":"B","LOCATION":"FBC"},
    {"AREA":"A/M MAINTENANCE","DIVISION":"B","LOCATION":"FBC"},
    {"AREA":"A/M PRODUCTION LCD DIVISION","DIV":"B","LOCATION":"SONY"},
    {"AREA":"AM VIZIO MAINTENANCE","DIVISION":"A","LOCATION":"FBC"},
    {"AREA":"AM VIZIO MAINTENANCE","DIVISION":"A","LOCATION":"FBC"},
    {"AREA":"AM VIZIO MAINTENANCE","DIVISION":"B","LOCATION":"FBC"},
    {"AREA":"AM VIZIO MAINTENANCE","DIVISION":"B","LOCATION":"FBX"},
    {"AREA":"AM VIZIO MAINTENANCE","DIVISION":"B","LOCATION":"FBX"},
    {"AREA":"AM VIZIO PRODUCTION","DIVISION":"B","LOCATION":"VIZIO"},
    {"AREA":"BUSINESS PLANNING","DIVISION":"B","LOCATION":"FBC"},
    {"AREA":"COMARKET F/A PRODUCTION DIRECT","DIVISION":"B","LOCATION":"FBC"},
    {"AREA":"CONTROLLERS","DIVISION":"B","LOCATION":"FBC"},
    ];

    let result = _.chain(data)
    	.groupBy('AREA')
    	.mapObject(area => _.groupBy(area, 'DIVISION'))
    	.mapObject(area => _.mapObject(area, div => _.groupBy(div, 'LOCATION')))
    	.value();
      
      console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

Upvotes: 1

Related Questions