2017-08-26 80 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; }