I have an arrangement that I need to take an average of a sum based on the user who attended an activity
I have an array where there is a list of activities, so suppose that the activity of the day "2022-11-25", which was a Friday, was attended by the entire organization, so the status isAttendanceAllOrg
is true for Therefore, the usersNotAttendance
array is empty, but on "2022-11-26" which was a Saturday, the entire organization did not attend, therefore the isAttendanceAllOrg
status would go to false and in usersNotAttendance
we would bring the users who did not attend the activity. Now we need to assign the minimum hours for each day, respectively.
But to do so we must take into account which modality the user belongs to. If the user is Fulltime, we use minTracked
, but if it is part time, we will use minTrackedPartime
. That is, the day "2022-11-25", which was a Friday. It was established that the minimum number of hours in Fulltime was "minTracked: 6" and the half-time ones were "minTrackedPartime: 2.5" and on Saturday a "minTracked: 3" for Fulltime and for Parttime a "minTrackedPartime: 3", so we need to find a way to average out minTracked
and minTrackedPartime
based on users who attended or not.
For example, as the day "2022-11-25" the user with the ID "1267" which is part time attended the activity, but the day "2022-11-26" did not, then his final average would be "totalTracked: 2.5", since the "minTrackedPartime" of the day "2022-11-26" is not being added because he did not attend. Quite the contrary, for that user, who did attend both activities on both days and that is full time, his average final would be the minTracked of date 25 plus the minTracked of date 26 which would be equal to 6 3 = 9.
So I have to be able to get a list of all the users with pertinent information on the modalities and their 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
}
}
]
And a list of activities
[
{
"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
}
]
And my output should be something like:
[{
"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
}]
I was thinking that maybe with a reduce in the array, where we are filtering by the user ID like they did in this example Reduce array of objects by user_id and sum certain values, but since the id that I am accessing is inside usersNotAttendance
it is difficult for me to understand how to filter it in such a way that it can be accessed.
CodePudding user response:
Here is a working example:
const list1 = [{
"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
}
}
];
const list2 = [{
"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
}
];
// Define the extractUserData function
function extractUserData(user, schedule) {
let user_id = user.id;
let username = user.username;
let modality = user.modality.name;
let totalTracked = 0;
// Check if the user is part of the schedule
if (schedule.isAttendanceAllOrg || schedule.usersNotAttendance.some(u => u.id_user === user.user_id)) {
// The user is part of the schedule, so determine the value of totalTracked
if (modality === "FULLTIME") {
totalTracked = schedule.minTracked;
} else if (modality === "PARTTIME") {
totalTracked = schedule.minTrackedPartime;
}
}
return {
user_id: user_id,
username: username,
modality: modality,
totalTracked: totalTracked
};
}
// Use the map method to extract the user data and create the output array
let output = list1.map(user => extractUserData(user, list2.find(schedule => schedule.date_start === "2022-11-26")));
// The output array now contains the required values for each user
console.log(output);
CodePudding user response:
I have managed to find the solution to my problem. In the end I was applying the logic wrong.
Since in reality I had to manage to list the users who did attend the schedule because they are the ones who are affected with a minimum of hours while those who do not attend are not taken into account, so adding the final average does not affect in my results.
So thinking about it, I did change the list. Now it is listusersAttendance
and here I list all those who go to the schedule. I also handle a status for in the event that everyone attends yes or yes always handling the status isAttendanceAllOrg
.
Because in reality it is what I really need; I need to know who attends, identify who, when they attended and when the 'minTracked' according to their modality.
With that modified, I iterated the array as @EugenSunic was kind enough to explain to me. It is only a forEach, so that for each element of the array the users that come within the schedule are being validated, and then I add some more validations that have to do with my use cases. But in itself, the question was how could iterate the arrangement and it was achieved.
Thank you very much!
DEMO
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>