我有一个任务对象数组,我想将它们转换为按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
分组并创建了一个嵌套的任务对象,但它只会为每个人添加一个任务。
哎呀。任何帮助真的很感激。
答案 0 :(得分:2)
这是一种功能性的方法(假设 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; }
答案 1 :(得分:1)
people[ownerID] = {};
person['tasks'] = {};
这些行不会检查之前是否存在具有相同键的对象,并且每次都会创建一个新对象。
尝试将其更改为
people[ownerID] = people[ownerID] || {};
person['tasks'] = person['tasks'] || {};
答案 2 :(得分:0)
只有一件事,任务应该是一个数组,因为对象无法在没有密钥的情况下持有另一个对象。
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);
答案 3 :(得分:0)
如果peroperty不存在,您可以使用逻辑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; }