Blog for web designers

Javascript group object by its properties

In this small article I will show you how you can easily group the object by its properties.

Below is the function code to group the object by its properties.

Common = {
 	_groupBy:function(list,props) {
		return list.reduce((a, b) => {(a[b[props]] = a[b[props]] || []).push(b);return a;}, {});
	},
}
Here I have this kind of format in my response where the date might be same but not the time. In this example I am going to use the sessionDate properties from the below response screenshot. 

With the below code now I can see new output in response. You can see how with date it is trying to create an array grouping other object with same date.

var sessionData=Common._groupBy(response.data.results,'sessionDate');
console.log(sessionData)

Santosh Shah

My name is Santosh Shah and blogs at santosh-shah.com


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.