2017-08-26 86 views
4

我有任务对象的数组,我想通过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罚款并创建一个嵌套的任务对象,但它只会为每个人添加一个任务。

Argh。任何帮助真的很感激。

+0

'tasks'应该是一个阵列,而不是一个对象。 –

+0

@NinaScholz为什么?如果他们是一个数组,我仍然可以使用人[100] .tasks [3] .title访问各个任务吗? – ginsberg

+0

我在想,你正在使用哪种数据类型。以上不是一个有效的例子。如果您需要反映'taskID'的属性,那么您还需要在示例中使用它。 –

回答

1
people[ownerID] = {}; 

person['tasks'] = {}; 

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

尝试将其更改为

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

person['tasks'] = person['tasks'] || {}; 
+0

令人惊叹。很简单。谢谢。 – ginsberg

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

+0

很好用。无论如何,你可以提供一些解释你的意思是功能依赖?我其实刚刚开始使用函数式编程。 –

+1

* ownerName *的功能依赖于* ownerID *,当它始终为true时,如果为ownerID赋予一个值,那么与* ownerName *关联的只有一个值,而不是两个。 – trincot

+0

不错,我喜欢它。 –

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