Skip to content
Advertisement

How to conditionally merge an array in Javascript?

I have a problem, I don’t know how to solve it, I will try not to give too much context and focus on the problem

A worker marks hours every day in his work, this is stored in the “dayli_data” array when the worker did not perform his hours then the “time_off” array is returned

I have two arrays that I don’t know how to unify, when dayli_data brings the information I must check if the user_id is different from the time_off and if it is, I must bring the list of “…dayli_data” + what it found in “…time_off”

So let’s say that the user with ID = 957706 on 10/29/2022 marked 8 hours

[
{
   user_id: 957706,
   tracked: 8,
   date: "2022-10-29"
},
{
   user_id: 1171637,
   tracked: 8,
  date: "2022-10-29"
}
]

But on 10/31/2022 he don’t mark hours, so in the record “dayli_data” it won’t come (just a short example of the data)

[
 {
   user_id: 1171637,
   tracked: 8,
  date: "2022-10-29"
} 
]

But it brings information in the time_off array

[{ user_id: 957706, reason: 'permission', tracked: 4, entry_date: "2022-10-31"}]

then I should be able to unify the arrays and return:

[
{
   user_id: 957706,
   tracked: 4,
   reason: 'permission',
  date_start: "2022-10-31",
  date: "2022-10-31"
},
{
   user_id: 1171637,
   tracked: 7,
   date: "2022-10-31"
}
]

How can I conditionally merge arrays?

I tried this:

let data_daily = [{
    "id": 6583194952,
    "date": "2022-10-31",
    "user_id": 1171637,
    "project_id": 2082652,
    "task_id": 111873721,
    "keyboard": 3891,
    "mouse": 8714,
    "overall": 11875,
    "tracked": 29494,
    "input_tracked": 29494,
    "manual": 0,
    "idle": 0,
    "resumed": 0,
    "billable": 17700,
    "created_at": "2022-10-31T14:14:05.300434Z",
    "updated_at": "2022-10-31T11:02:04.105898Z"
  },
  {
    "id": 6583205067,
    "date": "2022-10-31",
    "user_id": 1762437,
    "project_id": 2082652,
    "task_id": 111407896,
    "keyboard": 3843,
    "mouse": 13066,
    "overall": 15760,
    "tracked": 29275,
    "input_tracked": 29275,
    "manual": 0,
    "idle": 0,
    "resumed": 0,
    "billable": 29275,
    "created_at": "2022-10-31T14:15:54.284572Z",
    "updated_at": "2022-10-31T11:01:52.894533Z"
  }
]

let time_off = [{
    "user_id": 957963,
    "date_start": "2022-10-31",
    "date_end": "2022-11-05",
    "rol": "DEV",
    "reason": "Permiso",
    "created_at": "2022-10-31T15:46:42+00:00",
    "kpi": "permission",
    "activity": 60,
    "tracked": 4,
    "user": {
      "user_id": 957963,
      "name": "Isaac xxx",
      "email": "isaac.correo@c.com",
      "status": "active",
      "created_at": "2022-10-31T06:36:42.898255Z",
      "updated_at": "2022-10-31T13:59:08.095330Z",
      "daily_hours": 8,
      "username": "ISAAC XXX",
      "time_work": "FULLTIME",
      "job_position": "Junior"
    }
  },
  {
    "user_id": 957706,
    "date_start": "2022-10-31",
    "date_end": "2022-10-31",
    "rol": "DEV",
    "reason": "permission",
    "created_at": "2022-10-31T06:18:25+00:00",
    "kpi": "permission",
    "activity": 60,
    "tracked": 4,
    "user": {
      "user_id": 957706,
      "name": "Cesar xxxx",
      "email": "cesarz@c.com",
      "status": "active",
      "created_at": "2022-10-31T19:41:27.759263Z",
      "updated_at": "2022-10-31T15:20:46.296994Z",
      "daily_hours": 8,
      "username": "CESAR XXXX",
      "time_work": "FULLTIME",
      "job_position": "Junior "
    }
  }
]

const result = data_daily.map(element => {
  return time_off.length > 0 ? { ...time_off
  } : { ...element
  }
});

console.log(result)

But it only returns the elements of the time_off. What am I doing wrong?

Advertisement

Answer

From my understanding maybe you’re looking for something like this:

let data_daily = [{
  "user_id": 2,
  "tracked": 7,
  "date": "2022-10-31"
}]

let time_off = [{
  "user_id": 1,
  "reason": 'permission',
  "tracked": 4,
  "date_start": "2022-10-31"
}]


const result = time_off.reduce((resultArray, currTimeOff) => [...resultArray, currTimeOff], data_daily);

console.log(result)

Extra:

This is how JS reduce() works.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement