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

JavaScript

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)

JavaScript

But it brings information in the time_off array

JavaScript

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

JavaScript

How can I conditionally merge arrays?

I tried this:

JavaScript

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:

JavaScript

Extra:

This is how JS reduce() works.

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