将对象的平面数组转换为嵌套对象



我有一个任务对象数组,我想将它们转换为按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}];

这是我的预期输出:

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}
}       
},

};

我正在循环访问原始数据并分配每一行

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

这似乎按ownerID精细分组并创建一个嵌套的任务对象,但它只会为每个人添加一个任务。

唉。任何帮助真的非常感谢。

这是一种功能性方法(假设ownerName在功能上依赖于ownerID(:

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

people[ownerID] = {};
person['tasks'] = {};

这些行不会检查之前是否存在具有相同键的对象,并且每次都会创建一个新对象。

尝试将其更改为

people[ownerID] = people[ownerID] || {};
person['tasks'] = person['tasks'] || {};

只有一件事,任务应该是一个数组,因为对象在没有键的情况下无法容纳另一个对象。

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);

您可以使用逻辑 OR 作为默认运算符,如果性能不存在,则分配一个新对象。

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

最新更新