2017-07-15 93 views
0

我有一個對象,其中包含按週日嵌套和分組的任務。我無法改變它的結構。從該任務的「狀態」鍵值「==完成」返回第一個任務的最佳方式是什麼?返回第一個對象包含在一個JavaScript多級別嵌套的對象數組中的特定值

{ 
    "total_resource_cost":"20.0", 
    "total_unassigned_tasks_count":0, 
    "tasks":[ 
     { 
     "unassigned_tasks_count":0, 
     "resource_cost":"9.0", 
     "weekday":"Tuesday", 
     "date":"2017-07-11", 
     "tasks":[ 
      { 
       "id":6196, 
       "title":null, 
       "category":"Guest Ready Clean", 
       "status":"new", 
       "date":"2017-07-11", 
       "due_date":null, 
       "assigned_to":{ 
        "name":"David", 
        "id":2, 
        "mobile":"", 
        "teams":[ 

        ] 
       }, 
       "assigned_to_2":{ 
        "name":"David", 
        "id":2, 
        "mobile":"", 
        "teams":[ 

        ] 
       }, 
       "assigned_to_id":2, 
       "assigned_to_2_id":2, 
       "team":null, 
       "description":null, 
       "property":{ 
        "id":114, 
        "code":"WEB-1", 
        "lat":"51.45625339999999", 
        "lng":"-0.16253770000002987", 
        "postal_code":"SW11 6SF" 
       }, 
       "list":null, 
       "priority":3, 
       "created_by":null, 
       "created_at":"2017-07-11T16:39:34.944Z", 
       "updated_at":"2017-07-14T14:08:07.082Z", 
       "updated_by":{ 
        "id":2, 
        "name":"David", 
        "teams":[ 

        ] 
       }, 
       "notes":null, 
       "trigger_rule":"Booking followed by booking.", 
       "obsolete":true, 
       "resource_cost":"3.0" 
      }, 
      { 
       "id":6228, 
       "title":null, 
       "category":"Owner Leaving Clean", 
       "status":"new", 
       "date":"2017-07-11", 
       "due_date":null, 
       "assigned_to":{ 
        "name":"David the Housekeeper", 
        "id":220, 
        "mobile":"", 
        "teams":[ 

        ] 
       }, 
       "assigned_to_2":{ 
        "name":"David", 
        "id":2, 
        "mobile":"", 
        "teams":[ 

        ] 
       }, 
       "assigned_to_id":220, 
       "assigned_to_2_id":2, 
       "team":null, 
       "description":null, 
       "property":{ 
        "id":245, 
        "code":"ROL-1", 
        "lat":"51.51190219999999", 
        "lng":"-0.03558299999997416", 
        "postal_code":"E14 7JA" 
       }, 
       "list":null, 
       "priority":3, 
       "created_by":null, 
       "created_at":"2017-07-11T16:39:39.895Z", 
       "updated_at":"2017-07-14T14:07:58.565Z", 
       "updated_by":{ 
        "id":2, 
        "name":"David", 
        "teams":[ 

        ] 
       }, 
       "notes":null, 
       "trigger_rule":"Booking following block.", 
       "obsolete":true, 
       "resource_cost":"2.5" 
      }, 
      { 
       "id":6207, 
       "title":null, 
       "category":"Guest Ready Clean", 
       "status":"new", 
       "date":"2017-07-11", 
       "due_date":null, 
       "assigned_to":{ 
        "name":"David the Housekeeper", 
        "id":220, 
        "mobile":"", 
        "teams":[ 

        ] 
       }, 
       "assigned_to_2":{ 
        "name":"David", 
        "id":2, 
        "mobile":"", 
        "teams":[ 

        ] 
       }, 
       "assigned_to_id":220, 
       "assigned_to_2_id":2, 
       "team":null, 
       "description":null, 
       "property":{ 
        "id":126, 
        "code":"PRO-1", 
        "lat":"51.5375129", 
        "lng":"-0.042265000000043074", 
        "postal_code":"E9 7DS" 
       }, 
       "list":null, 
       "priority":3, 
       "created_by":null, 
       "created_at":"2017-07-11T16:39:35.963Z", 
       "updated_at":"2017-07-14T14:07:54.920Z", 
       "updated_by":{ 
        "id":2, 
        "name":"David", 
        "teams":[ 

        ] 
       }, 
       "notes":null, 
       "trigger_rule":"Booking followed by booking.", 
       "obsolete":true, 
       "resource_cost":"2.0" 
      }, 
      { 
       "id":6202, 
       "title":null, 
       "category":"Guest Ready Clean", 
       "status":"new", 
       "date":"2017-07-11", 
       "due_date":null, 
       "assigned_to":{ 
        "name":"David", 
        "id":2, 
        "mobile":"", 
        "teams":[ 

        ] 
       }, 
       "assigned_to_2":{ 
        "name":"David the Housekeeper", 
        "id":220, 
        "mobile":"+", 
        "teams":[ 

        ] 
       }, 
       "assigned_to_id":2, 
       "assigned_to_2_id":220, 
       "team":null, 
       "description":null, 
       "property":{ 
        "id":234, 
        "code":"CAD-2", 
        "lat":"51.4924164", 
        "lng":"-0.16394930000001295", 
        "postal_code":"SW3 2PP" 
       }, 
       "list":null, 
       "priority":3, 
       "created_by":null, 
       "created_at":"2017-07-11T16:39:35.307Z", 
       "updated_at":"2017-07-14T14:07:49.911Z", 
       "updated_by":{ 
        "id":2, 
        "name":"David", 
        "teams":[ 

        ] 
       }, 
       "notes":null, 
       "trigger_rule":"Booking followed by booking.", 
       "obsolete":true, 
       "resource_cost":"1.5" 
      } 
     ] 
     }, 
     { 
     "unassigned_tasks_count":0, 
     "resource_cost":"11.0", 
     "weekday":"Wednesday", 
     "date":"2017-07-12", 
     "tasks":[ 
      { 
       "id":6148, 
       "title":null, 
       "category":"Guest Ready Clean", 
       "status":"new", 
       "date":"2017-07-12", 
       "due_date":null, 
       "assigned_to":{ 
        "name":"David", 
        "id":2, 
        "mobile":"", 
        "teams":[ 

        ] 
       }, 
       "assigned_to_2":null, 
       "assigned_to_id":2, 
       "assigned_to_2_id":null, 
       "team":null, 
       "description":null, 
       "property":{ 
        "id":116, 
        "code":"SAR-1", 
        "lat":null, 
        "lng":null, 
        "postal_code":null 
       }, 
       "list":null, 
       "priority":3, 
       "created_by":null, 
       "created_at":"2017-07-11T16:39:28.736Z", 
       "updated_at":"2017-07-14T15:30:15.665Z", 
       "updated_by":{ 
        "id":2, 
        "name":"David", 
        "teams":[ 

        ] 
       }, 
       "notes":null, 
       "trigger_rule":"Booking followed by booking.", 
       "obsolete":true, 
       "resource_cost":"5.5" 
      }, 
      { 
       "id":6193, 
       "title":null, 
       "category":"Guest Ready Clean", 
       "status":"new", 
       "date":"2017-07-12", 
       "due_date":null, 
       "assigned_to":{ 
        "name":"David", 
        "id":2, 
        "mobile":"", 
        "teams":[ 

        ] 
       }, 
       "assigned_to_2":null, 
       "assigned_to_id":2, 
       "assigned_to_2_id":null, 
       "team":null, 
       "description":null, 
       "property":{ 
        "id":216, 
        "code":"MAC-1", 
        "lat":"51.5480904", 
        "lng":"-0.11545330000001286", 
        "postal_code":"N7 8QY" 
       }, 
       "list":null, 
       "priority":3, 
       "created_by":null, 
       "created_at":"2017-07-11T16:39:34.435Z", 
       "updated_at":"2017-07-14T15:30:03.989Z", 
       "updated_by":{ 
        "id":2, 
        "name":"David", 
        "teams":[ 

        ] 
       }, 
       "notes":null, 
       "trigger_rule":"A stay has ended. Unclear what the next event is.", 
       "obsolete":true, 
       "resource_cost":"3.5" 
      }, 
      { 
       "id":6238, 
       "title":null, 
       "category":"Guest Ready Clean", 
       "status":"new", 
       "date":"2017-07-12", 
       "due_date":null, 
       "assigned_to":{ 
        "name":"David", 
        "id":2, 
        "mobile":"", 
        "teams":[ 

        ] 
       }, 
       "assigned_to_2":null, 
       "assigned_to_id":2, 
       "assigned_to_2_id":null, 
       "team":null, 
       "description":null, 
       "property":{ 
        "id":252, 
        "code":"WEL-1", 
        "lat":"51.4853737", 
        "lng":"0.03197999999997592", 
        "postal_code":"SE7 7PH" 
       }, 
       "list":null, 
       "priority":3, 
       "created_by":null, 
       "created_at":"2017-07-11T16:39:40.933Z", 
       "updated_at":"2017-07-14T15:29:53.195Z", 
       "updated_by":{ 
        "id":2, 
        "name":"David", 
        "teams":[ 

        ] 
       }, 
       "notes":null, 
       "trigger_rule":"Booking followed by booking.", 
       "obsolete":true, 
       "resource_cost":"2.0" 
      } 
     ] 
     } 
    ] 
} 

回答

2

在阿布舍克Soni的回答略有改善。我們可以使用.find返回數組中返回true的第一項,而不是過濾所有完成的任務。

const firstCompletedTasked = response.tasks[0].tasks.find(task => task.status === 'complete'); 
+0

如果您有興趣,請解釋爲什麼這是一項改進?速度? – larpo

+0

速度是一個小的因素,因爲查找會盡快返回匹配,而過濾器將處理整個數組。對我而言,重要的是find會返回對象,而不是數組。 –

0

假設保存該對象在名爲response變量:

let firstInCompleteTask = response.tasks[0].tasks.filter((task)=>{ 
    return task.status !== 'complete' 
})[0]; 

另外,第一tasks陣列具有兩個對象,然後在他們一個任務陣列。上面的代碼返回第一個Object的第一個不完整的任務。

如果你想從兩個對象未完成的任務,你必須使用類似:

let incompleteTasks = []; 
for(i = 0; i < response.tasks.length; i++){ 
    let firstInCompleteTask = response.tasks[i].tasks.filter((task)=>{ 
     return task.status !== 'complete' 
    })[0]; 

    incompleteTasks.push(fistInCompleteTask); 
} 
相關問題