簡體   English   中英

按 ID 和 JavaScript 中的項目減少數組

[英]Reduce an array by ID and by item in JavaScript

我有一個安排,我需要根據參加活動的用戶取一個總和的平均值

我有一個數組,其中有一個活動列表,因此假設整個組織都參加了“2022-11-25”這一天的活動,這是一個星期五,因此狀態isAttendanceAllOrg為 true 因此, usersNotAttendance數組為空,但在星期六的“2022-11-26”,整個組織都沒有參加,因此isAttendanceAllOrg狀態將為 go 為 false,在usersNotAttendance中,我們將帶入未參加活動的用戶。 現在我們需要分別分配每天的最短時間。

但要做到這一點,我們必須考慮用戶屬於哪種模式。 如果用戶是全職,我們使用minTracked ,但如果是兼職,我們將使用minTrackedPartime 也就是說,“2022-11-25”這一天是星期五。 確定全日制的最低小時數為“minTracked:6” ,半日制小時數為“minTrackedPartime:2.5” ,周六全日制為“minTracked:3”,兼職為“minTrackedPartime:3” ,所以我們需要找到一種方法,根據參加或不參加的用戶來平均minTrackedminTrackedPartime

例如,由於 ID 為“1267”的兼職用戶在“2022-11-25”這一天參加了活動,但在“2022-11-26”這一天沒有參加,那么他的最終平均值將是“totalTracked : 2.5” ,因為“2022-11-26”這一天的“minTrackedPartime”沒有被添加,因為他沒有參加。 恰恰相反,對於在兩天內都參加這兩項活動並且是全職用戶的用戶,他的平均最終結果將是日期 25 的 minTracked 加上日期 26 的 minTracked 等於 6 + 3 = 9。

所以我必須能夠獲得所有用戶的列表,其中包含有關模式及其 ID 的相關信息:

[
    {
        "id": 1278,
        "name": "User18",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 4,
            "job_position": "Trainee Fulltime",
            "created_at": "2022-11-02T21:54:17.963682+00:00",
            "status": true
        }
    },
    {
        "id": 1273,
        "username": "User17",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 31,
            "tittle": "SAC",
            "priority": 1,
            "type": 2,
            "created_at": "2022-11-02T21:05:33+00:00",
            "status": true
        },
        "job_position": {
            "id": 4,
            "job_position": "Trainee Fulltime",
            "created_at": "2022-11-02T21:54:17.963682+00:00",
            "status": true
        }
    },
    {
        "id": 1288,
        "username": "User16",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 4,
            "job_position": "Trainee Fulltime",
            "created_at": "2022-11-02T21:54:17.963682+00:00",
            "status": true
        }
    },
    {
        "id": 1279,
        "username": "User15",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 4,
            "job_position": "Trainee Fulltime",
            "created_at": "2022-11-02T21:54:17.963682+00:00",
            "status": true
        }
    },
    {
        "id": 1287,
        "username": "User14",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 4,
            "job_position": "Trainee Fulltime",
            "created_at": "2022-11-02T21:54:17.963682+00:00",
            "status": true
        }
    },
    {
        "id": 1277,
        "username": "User13",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 4,
            "job_position": "Trainee Fulltime",
            "created_at": "2022-11-02T21:54:17.963682+00:00",
            "status": true
        }
    },
    {
        "id": 1270,
        "username": "User12",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 2,
            "job_position": "Junior",
            "created_at": "2022-11-02T21:53:41.024661+00:00",
            "status": false
        }
    },
    {
        "id": 1272,
        "username": "User11",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 31,
            "tittle": "SAC",
            "priority": 1,
            "type": 2,
            "created_at": "2022-11-02T21:05:33+00:00",
            "status": true
        },
        "job_position": {
            "id": 4,
            "job_position": "Trainee Fulltime",
            "created_at": "2022-11-02T21:54:17.963682+00:00",
            "status": true
        }
    },
    {
        "id": 1282,
        "name": "User10",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 4,
            "job_position": "Trainee Fulltime",
            "created_at": "2022-11-02T21:54:17.963682+00:00",
            "status": true
        }
    },
    {
        "id": 1266,
        "username": "User09",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 5,
            "job_position": "Senior",
            "created_at": "2022-11-02T21:54:33.127234+00:00",
            "status": true
        }
    },
    {
        "id": 1276,
        "username": "User08",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 2,
            "job_position": "Junior",
            "created_at": "2022-11-02T21:53:41.024661+00:00",
            "status": false
        }
    },
    {
        "id": 1264,
        "username": "User08",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 5,
            "job_position": "Senior",
            "created_at": "2022-11-02T21:54:33.127234+00:00",
            "status": true
        }
    },
    {
        "id": 1281,
        "username": "User07",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 31,
            "tittle": "SAC",
            "priority": 1,
            "type": 2,
            "created_at": "2022-11-02T21:05:33+00:00",
            "status": true
        },
        "job_position": {
            "id": 4,
            "job_position": "Trainee Fulltime",
            "created_at": "2022-11-02T21:54:17.963682+00:00",
            "status": true
        }
    },
    {
        "id": 1268,
        "username": "User06",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 2,
            "job_position": "Junior",
            "created_at": "2022-11-02T21:53:41.024661+00:00",
            "status": false
        }
    },
    {
        "id": 1269,
        "username": "User05",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 2,
            "job_position": "Junior",
            "created_at": "2022-11-02T21:53:41.024661+00:00",
            "status": false
        }
    },
    {
        "id": 1265,
        "username": "User04",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 5,
            "job_position": "Senior",
            "created_at": "2022-11-02T21:54:33.127234+00:00",
            "status": true
        }
    },
    {
        "id": 1292,
        "username": "User03",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 32,
            "tittle": "DESIGN",
            "priority": 2,
            "type": 3,
            "created_at": "2022-11-02T21:05:51+00:00",
            "status": true
        },
        "job_position": {
            "id": 4,
            "job_position": "Trainee Fulltime",
            "created_at": "2022-11-02T21:54:17.963682+00:00",
            "status": true
        }
    },
    {
        "id": 1274,
        "username": "User02",
        "modality": {
            "id": 1,
            "name": "FULLTIME",
            "weekly_hours": 44,
            "daily_hours": 8,
            "monthly_hours": 192,
            "created_at": "2022-11-02T20:46:07.169271+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 2,
            "job_position": "Junior",
            "created_at": "2022-11-02T21:53:41.024661+00:00",
            "status": false
        }
    },
    {
        "id": 1267,
        "username": "User01",
        "modality": {
            "id": 2,
            "name": "PARTTIME",
            "weekly_hours": 24,
            "daily_hours": 4,
            "monthly_hours": 104,
            "created_at": "2022-11-02T20:47:13.961156+00:00",
            "status": true
        },
        "rol": {
            "id": 30,
            "tittle": "DEV",
            "priority": 0,
            "type": 1,
            "created_at": "2022-11-02T21:05:08+00:00",
            "status": true
        },
        "job_position": {
            "id": 5,
            "job_position": "Senior",
            "created_at": "2022-11-02T21:54:33.127234+00:00",
            "status": true
        }
    }
]

以及活動清單

[
    {
        "id": 18,
        "created_at": "2022-11-30T22:07:29.47307+00:00",
        "date_start": "2022-11-25",
        "date_end": "2022-11-25",
        "indicator_hours": 3,
        "id_org": 1,
        "minTracked": 6,
        "minActivity": 20,
        "isAttendanceAllOrg": true,
        "usersNotAttendance": [],
        "description": "ACTIVIDAD DE PRUEBA",
        "type_of_schedule": "activity",
        "indicator_activity": 2,
        "minTrackedPartime": 2.5,
        "minActivityPartime": 10
    },
    {
        "id": 20,
        "created_at": "2022-11-30T22:41:16.081726+00:00",
        "date_start": "2022-11-26",
        "date_end": "2022-11-26",
        "indicator_hours": 3,
        "id_org": 1,
        "minTracked": 3,
        "minActivity": 10,
        "isAttendanceAllOrg": false,
        "usersNotAttendance": [
            {
                "isWork": true,
                "id_user": 1267
            },
            {
                "isWork": true,
                "id_user": 1274
            },
            {
                "isWork": true,
                "id_user": 1292
            }
        ],
        "description": "ACTIVIDAD SIN ASISTENTES",
        "type_of_schedule": "activity",
        "indicator_activity": 3,
        "minTrackedPartime": 3,
        "minActivityPartime": 20
    }
]

而我的 output 應該是這樣的:

[{
   "user_id": 1267,
   "username": "User01",
   "modality": "PARTTIME",
   "totalTracked": 2.5
},
{
   "user_id": 1274,
   "username": "User02",
   "modality": "FULLTIME",
   "totalTracked": 6
},
{
   "user_id": 1292,
   "username": "User03",
"modality": "FULLTIME",
   "totalTracked": 6
},
{
   "user_id": 1268,
   "username": "User06",
"modality": "FULLTIME",
   "totalTracked": 9
},
{
   "user_id": 1264,
   "username": "User08",
"modality": "FULLTIME",
   "totalTracked": 9
},
{
   "user_id": 1277,
   "username": "User13",
"modality": "FULLTIME",
   "totalTracked": 9
}
.
.
.
{
   "user_id": 1278,
   "username": "User18",
"modality": "FULLTIME",
   "totalTracked": 9
}]

我在想,也許在數組中減少,我們像在這個例子中那樣按用戶 ID 過濾通過 user_id 減少對象數組並對某些值求和,但由於我正在訪問的 id 在usersNotAttendance內部,所以它是我很難理解如何以可以訪問的方式對其進行過濾。

我設法找到了解決問題的方法。 最后我應用了錯誤的邏輯。

因為實際上我不得不設法列出確實參加了日程安排的用戶,因為他們是受影響時間最少的用戶,而沒有參加的人則沒有被考慮在內,所以添加最終平均值不會影響我的結果。

所以想了想,我確實改了名單。 現在是listusersAttendance ,我在這里列出所有 go 的人。 如果每個人都參加是或總是處理狀態isAttendanceAllOrg ,我也會處理狀態。

因為實際上這是我真正需要的; 我需要知道誰參加,確定誰,他們何時參加以及根據他們的方式進行“minTracked”。

修改后,我迭代了數組,因為@EugenSunic 很友好地向我解釋。 它只是一個 forEach,因此對於數組中的每個元素,都會對排在計划內的用戶進行驗證,然后我添加一些與我的用例有關的驗證。 但就其本身而言,問題是如何迭代安排並實現。

非常感謝你!

演示

 const attendance_schedule = [ { "id": 20, "created_at": "2022-11-30T22:41:16.081726+00:00", "date_start": "2022-11-26", "date_end": "2022-11-26", "indicator_hours": 3, "id_org": 1, "minTracked": 0, "minActivity": 0, "isAttendanceAllOrg": false, "listusersAttendance": [ { "isWork": true, "id_user": 1274 }, { "isWork": true, "id_user": 1292 }, { "isWork": true, "id_user": 1265 }, { "isWork": true, "id_user": 1269 }, { "isWork": true, "id_user": 1268 }, { "isWork": true, "id_user": 1281 }, { "isWork": true, "id_user": 1264 }, { "isWork": true, "id_user": 1276 }, { "isWork": true, "id_user": 1266 }, { "isWork": true, "id_user": 1282 }, { "isWork": true, "id_user": 1272 }, { "isWork": true, "id_user": 1270 }, { "isWork": true, "id_user": 1277 }, { "isWork": true, "id_user": 1289 }, { "isWork": true, "id_user": 1279 }, { "isWork": true, "id_user": 1288 }, { "isWork": true, "id_user": 1273 }, { "isWork": true, "id_user": 1278 } ], "description": "ACTIVIDAD SIN ASISTENTES", "type_of_schedule": "activity", "indicator_activity": 3, "minTrackedPartime": 0, "minActivityPartime": 0, "minTrackedPartimeW": 3, "minTrackedFulltimeW": 3, "minActivityPartimeW": 50, "minActivityFulltimeW": 50 }, { "id": 18, "created_at": "2022-11-30T22:07:29.47307+00:00", "date_start": "2022-11-25", "date_end": "2022-11-25", "indicator_hours": 3, "id_org": 1, "minTracked": 6, "minActivity": 20, "isAttendanceAllOrg": false, "listusersAttendance":[ { "isWork": true, "id_user": 1274 }, { "isWork": true, "id_user": 1292 }, { "isWork": true, "id_user": 1265 }, { "isWork": true, "id_user": 1269 }, { "isWork": true, "id_user": 1268 }, { "isWork": true, "id_user": 1281 }, { "isWork": true, "id_user": 1264 }, { "isWork": true, "id_user": 1276 }, { "isWork": true, "id_user": 1266 }, { "isWork": true, "id_user": 1282 }, { "isWork": true, "id_user": 1272 }, { "isWork": true, "id_user": 1270 }, { "isWork": true, "id_user": 1277 }, { "isWork": true, "id_user": 1289 }, { "isWork": true, "id_user": 1279 }, { "isWork": true, "id_user": 1288 }, { "isWork": true, "id_user": 1273 }, { "isWork": true, "id_user": 1278 }, { "isWork": true, "id_user": 1267 } ], "description": "ACTIVIDAD DE PRUEBA", "type_of_schedule": "activity", "indicator_activity": 2, "minTrackedPartime": 2.5, "minActivityPartime": 10, "minTrackedPartimeW": null, "minTrackedFulltimeW": null, "minActivityPartimeW": null, "minActivityFulltimeW": null } ] const users = [ { "id": 1278, "username": "User18", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 4, "job_position": "Trainee Fulltime", "created_at": "2022-11-02T21:54:17.963682+00:00", "status": true } }, { "id": 1273, "username": "User17", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 31, "tittle": "SAC", "priority": 1, "type": 2, "created_at": "2022-11-02T21:05:33+00:00", "status": true }, "job_position": { "id": 4, "job_position": "Trainee Fulltime", "created_at": "2022-11-02T21:54:17.963682+00:00", "status": true } }, { "id": 1288, "username": "User16", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 4, "job_position": "Trainee Fulltime", "created_at": "2022-11-02T21:54:17.963682+00:00", "status": true } }, { "id": 1279, "username": "User15", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 4, "job_position": "Trainee Fulltime", "created_at": "2022-11-02T21:54:17.963682+00:00", "status": true } }, { "id": 1287, "username": "User14", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 4, "job_position": "Trainee Fulltime", "created_at": "2022-11-02T21:54:17.963682+00:00", "status": true } }, { "id": 1277, "username": "User13", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 4, "job_position": "Trainee Fulltime", "created_at": "2022-11-02T21:54:17.963682+00:00", "status": true } }, { "id": 1270, "username": "User12", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 2, "job_position": "Junior", "created_at": "2022-11-02T21:53:41.024661+00:00", "status": false } }, { "id": 1272, "username": "User11", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 31, "tittle": "SAC", "priority": 1, "type": 2, "created_at": "2022-11-02T21:05:33+00:00", "status": true }, "job_position": { "id": 4, "job_position": "Trainee Fulltime", "created_at": "2022-11-02T21:54:17.963682+00:00", "status": true } }, { "id": 1282, "name": "User10", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 4, "job_position": "Trainee Fulltime", "created_at": "2022-11-02T21:54:17.963682+00:00", "status": true } }, { "id": 1266, "username": "User09", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 5, "job_position": "Senior", "created_at": "2022-11-02T21:54:33.127234+00:00", "status": true } }, { "id": 1276, "username": "User08", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 2, "job_position": "Junior", "created_at": "2022-11-02T21:53:41.024661+00:00", "status": false } }, { "id": 1264, "username": "User08", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 5, "job_position": "Senior", "created_at": "2022-11-02T21:54:33.127234+00:00", "status": true } }, { "id": 1281, "username": "User07", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 31, "tittle": "SAC", "priority": 1, "type": 2, "created_at": "2022-11-02T21:05:33+00:00", "status": true }, "job_position": { "id": 4, "job_position": "Trainee Fulltime", "created_at": "2022-11-02T21:54:17.963682+00:00", "status": true } }, { "id": 1268, "username": "User06", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 2, "job_position": "Junior", "created_at": "2022-11-02T21:53:41.024661+00:00", "status": false } }, { "id": 1269, "username": "User05", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 2, "job_position": "Junior", "created_at": "2022-11-02T21:53:41.024661+00:00", "status": false } }, { "id": 1265, "username": "User04", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 5, "job_position": "Senior", "created_at": "2022-11-02T21:54:33.127234+00:00", "status": true } }, { "id": 1292, "username": "User03", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 32, "tittle": "DESIGN", "priority": 2, "type": 3, "created_at": "2022-11-02T21:05:51+00:00", "status": true }, "job_position": { "id": 4, "job_position": "Trainee Fulltime", "created_at": "2022-11-02T21:54:17.963682+00:00", "status": true } }, { "id": 1274, "username": "User02", "modality": { "id": 1, "name": "FULLTIME", "weekly_hours": 44, "daily_hours": 8, "monthly_hours": 192, "created_at": "2022-11-02T20:46:07.169271+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 2, "job_position": "Junior", "created_at": "2022-11-02T21:53:41.024661+00:00", "status": false } }, { "id": 1267, "username": "User01", "modality": { "id": 2, "name": "PARTTIME", "weekly_hours": 24, "daily_hours": 4, "monthly_hours": 104, "created_at": "2022-11-02T20:47:13.961156+00:00", "status": true }, "rol": { "id": 30, "tittle": "DEV", "priority": 0, "type": 1, "created_at": "2022-11-02T21:05:08+00:00", "status": true }, "job_position": { "id": 5, "job_position": "Senior", "created_at": "2022-11-02T21:54:33.127234+00:00", "status": true } } ]; let isActivity = []; let isHoliday= []; let isWorkingDays = []; let newArrayOfAttendanceInWorkingDays = []; let isWeekendsDays = []; let newArrayOfAttendanceInWeekendDays = []; let newListOfSchedule= []; users.map(user => { let user_id = user.id; let username = user.username; let modality = user.modality.name; let totalTracked= 0; let newDailyHours = 0; attendance_schedule.forEach(schedule => { const dateMomentForSchedule = moment(schedule.date_start); const dailyHoursInFulltime = 8; const dailyHoursInParttime = 4; const dailyHoursInFulltimeInWeekend = 4; const dailyHoursInParttimeInWeekend = 4; // Check if the user is part of the schedule if ((schedule.isAttendanceAllOrg === false && schedule.listusersAttendance.some(u => u.id_user === user.id))) { // The user is part of the schedule, so determine the value of totalTracked // revisar las modalidad para asignar el nuevo daily hours que depende de la calendarizacion if (modality === "FULLTIME") { totalTracked = (dateMomentForSchedule.day() === 0 || dateMomentForSchedule.day() === 6)? schedule.minTrackedFulltimeW: schedule.minTracked; newDailyHours = (dateMomentForSchedule.day() === 0 || dateMomentForSchedule.day() === 6)? (dailyHoursInFulltimeInWeekend - totalTracked): (dailyHoursInFulltime - totalTracked)?? 0; } else if (modality === "PARTTIME") { totalTracked = (dateMomentForSchedule.day() === 0 || dateMomentForSchedule.day() === 6)? schedule.minTrackedPartimeW: schedule.minTrackedPartime; newDailyHours = (dateMomentForSchedule.day() === 0 || dateMomentForSchedule.day() === 6)? (dailyHoursInParttimeInWeekend - totalTracked): (dailyHoursInParttime - totalTracked)?? 0; } } newListOfSchedule.push({ user_id: user_id, username: username, modality: modality, totalTracked: totalTracked?? 0, daily_hours: newDailyHours }); }); }); console.log(newListOfSchedule)
 <script src="https://momentjs.com/downloads/moment.js"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM