Skip to content
Advertisement

Search data from array of object inside foreach loop

I have 3 array. The first one is array employee, second is array dateRange(Y-m-d), and the third is array attendance

The array is like this:

Array Employee

JavaScript

Array dateRange

JavaScript

Array Attendance (both in & out)

JavaScript

What I want to do is to get attendance data from array attendance where employee_id is the same as employee_id in array employee.

What I’ve done is first I loop the array employee and then inside the loop I add another nested foreach to loop array dateRange. And then inside foreach array dateRange I’ve a code to search data.

JavaScript

But the code above still not working, I don’t get the data correctly. I know there’s something wrong in my code, but I don’t know what it is.

Advertisement

Answer

One way of solving this is by mapping and filtering your arrays like this:

JavaScript

By doing that the result of $intersection would be:

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