I have an array of objects that I want to sort by some data from grouping properties, and a line telling me which property to group (for example: "Organization" or "Organization .Name")
I need to write a function that takes data that looks like beforeData and returns afterData
Entrance:
beforeData = [ {'name':'John Doe', 'Id':1, 'Organizations':[{'Id':12, 'LongName': 'Group A'},{'Id':13, 'LongName': 'Group B'}]}, {'name':'FooBar', 'Id':2, 'Organizations':[{'Id':13, 'LongName': 'Group B'},{'Id':14, 'LongName': 'Group C'}]}, {'name':'Kristine Bell', 'Id':3, 'Organizations':[{'Id':12, 'LongName': 'Group A'}]}, {'name':'Adrian P', 'Id':4, 'Organizations':[{'Id':12, 'LongName': 'Group A'}]} ]
Output:
afterData = [ { 'Group': 'Group A', 'entities':[ {'name':'Adrian P', 'Id':4, 'Organizations':[{'Id':12, 'LongName': 'Group A'}]}, {'name':'Kristine Bell', 'Id':3, 'Organizations':[{'Id':12, 'LongName': 'Group A'}]}, {'name':'John Doe', 'Id':1, 'Organizations':[{'Id':12, 'LongName': 'Group A'},{'Id':13, 'LongName': 'Group B'}]}] }, { 'Group': 'Group B', 'entities':[ {'name':'John Doe', 'Id':1, 'Organizations':[{'Id':12, 'LongName': 'Group A'},{'Id':13, 'LongName': 'Group B'}]}, {'name':'FooBar', 'Id':2, 'Organizations':[{'Id':13, 'LongName': 'Group B'},{'Id':13, 'LongName': 'Group C'}]},] }, { 'Group': 'Group C', 'entities':[ {'name':'FooBar', 'Id':2, 'Organizations':[{'Id':13, 'LongName': 'Group B'},{'Id':13, 'LongName': 'Group C'}]},] } ]
How can i do this? My current attempts are extremely bloated and forever require large datasets.
Special kicker! : A function that solves this problem should be able to solve it without knowing in advance whether the "group by property" is 1 or 2 deep (for example: "Organization" or 'Organization.LongName').