Archive

group by count json query

I am playing with some JSON type of data, and need to do some reports, in particular a "group by count" query, so here is what I came up with:

var groupByCount = function(list, name){
	
	var groups = {};
				
	list.forEach(function(d){
		if(groups[d[name]]){
			groups[d[name]]++;
		} else {
			groups[d[name]] = 1;
		}
	});

	return groups;
};

(I am using the '.forEach' method available in node.js, but this could be replaced with a regular loop and used on the client.

Comments: