var data = [{ "name": "jim", "color": "blue", "age": "22" }, { "name": "Sam", "color": "blue", "age": "33" }, { "name": "eddie", "color": "green", "age": "77" }]; function groupBy(dataToGroupOn, fieldNameToGroupOn, fieldNameForGroupName, fieldNameForChildren) { var result = _.chain(dataToGroupOn) .groupBy(fieldNameToGroupOn) .toPairs() .map(function (currentItem) { return _.zipObject([fieldNameForGroupName, fieldNameForChildren], currentItem); }) .value(); return result; } var result = groupBy(data, 'color', 'colorId', 'users'); console.log(result);
Search thousands of free JavaScript snippets that you can quickly copy and paste into your web pages. Get free JavaScript tutorials, references, code, menus, calendars, popup windows, games, and much more.
Generic groupBy using lodash
Subscribe to:
Post Comments (Atom)
What is Security Engineering? Part 2.
A broad overview of the ever-developing security engineering field; a domain that can feel intimidating to some software engineers. With Nie...
No comments:
Post a Comment