Collection of javascript helper functions.
List will be expanded
Array.prototype.groupBy = function(prop) { | |
return this.reduce(function(groups, item) { | |
var val = item[prop]; | |
groups[val] = groups[val] || []; | |
groups[val].push(item); | |
return groups; | |
}, {}); | |
} | |
var myList = [ | |
{time: '12:00', location: 'mall' }, | |
{time: '9:00', location: 'store' }, | |
{time: '9:00', location: 'mall' }, | |
{time: '12:00', location: 'store' }, | |
{time: '12:00', location: 'market' }, | |
]; | |
var byTime = myList.groupBy('time'); | |
/** | |
byTime = { | |
'9:00': [ | |
{time: '9:00', location: 'store' }, | |
{time: '9:00', location: 'mall' }, | |
], | |
'12:00': [ | |
{time: '12:00', location: 'mall' }, | |
{time: '12:00', location: 'store' }, | |
{time: '12:00', location: 'market'} | |
] | |
} | |
*/ | |