I have a list of task items that I need to organize into a structured object based on the ownerID
var tasks = [
{taskID: "1", title: "task1", ownerID: "100", ownerName: "John", allocation: 80},
{taskID: "2", title: "task2", ownerID: "110", ownerName: "Sarah", allocation: 50},
{taskID: "3", title: "task3", ownerID: "110", ownerName: "Sarah", allocation: 50},
{taskID: "4", title: "task4", ownerID: "120", ownerName: "Mike", allocation: 25},
{taskID: "5", title: "task5", ownerID: "120", ownerName: "Mike", allocation: 45}];
This is the desired structure:
var people = {
100: {
ownerName: "John",
tasks: [
{taskID: "1", title: "task1", allocation: 80}
]
},
110: {
ownerName: "Sarah",
tasks: [
{taskID: "2", title: "task2", allocation: 50},
{taskID: "3", title: "task3", allocation: 50}
]
},
120: {
ownerName: "Mike",
tasks: [
{taskID: "4", title: "task4", allocation: 25},
{taskID: "5", title: "task5", allocation: 45}
]
}
};
I am iterating through the original data and assigning each entry
people[ownerID] = {};
person = people[ownerID];
person['ownerName'] = ownerName;
person['tasks'] = [];
person[taskID] = {};
task = person[taskId];
task['taskID'] = taskID;
The current setup groups by ownerID
correctly but only includes one task per individual.
Please assist. Thank you!