Convert a flat array of objects to nested objects

I have an array of task objects and I want to convert them to a multidimensional object grouped by 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 my expected result:

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 look at the source data and assigning each row

people[ownerID] = {};
person = people[ownerID];
person['ownerName'] = ownerName;
person['tasks'] = {};
person[taskID] = {};
task = person[taskId];
task['taskID'] = taskID;

It seems that it is grouped with ownerIDfine and creates a nested task object, but it will add only one task for each person.

Argh. Any help really appreciated.

+4
source share
4 answers
people[ownerID] = {};

person['tasks'] = {};

These lines do not check if an object with the same key exists before and each time create a new object.

Try changing it to

people[ownerID] = people[ownerID] || {};

person['tasks'] = person['tasks'] || {};
+1
source

(, _ ):

const 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}];

const result = tasks.reduce( (acc, {taskID, title, ownerID, ownerName, allocation }) => {
    (acc[ownerID] = acc[ownerID] || { ownerName, tasks: [] })
        .tasks.push( { taskID, title, allocation } );
    return acc;
}, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
+2

, , .

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}];

var transformed = tasks.reduce((obj, item)=> {
    if(!obj[item.ownerID]) {
        obj[item.ownerID] = {};
    }
    obj[item.ownerID].ownerName = item.ownerName;
    if(!obj[item.ownerID].tasks) {
        obj[item.ownerID].tasks = [];
    }
    obj[item.ownerID].tasks.push({taskID: item.taskID, title: item.title, allocation: item.allocation})
    return obj;
}, {})
console.log(transformed);
0
source

You can use logical OR as the default ans operator to assign a new object if peroperty does not exist.

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 }],
    people = {};

tasks.forEach(({ taskID, title, ownerID, ownerName, allocation }) => {
    people[ownerID] = people[ownerID] || { ownerName, tasks: {} };
    people[ownerID].tasks[taskID] = people[ownerID].tasks[taskID] || { taskID, title, allocation };
});

console.log(people);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run code
0
source

Source: https://habr.com/ru/post/1684531/


All Articles