How Can I Remove Employees From The Array That Have Enddate Property Higher Than Current Selected Month?
The data is coming from Google Sheets API, hence employees.stratdate, employees.enddate etc. I need to be able to display only the employees that used to work in a selected month.
Solution 1:
how about simply adding the employees that correspond during the first loop:
employees.forEach(emp => {
if (testArr.some(item =>moment(emp.startdate).format('M-YYYY') == item) &&
testArr.some(item => (moment(emp.enddate).format('M-YYYY') > item))) {
result.push(emp);
}
});
Solution 2:
I hope this help you,
There is a method in javascript arrays that will help you. As MAP, that iterate every item on an array, exits the array method filter
On the last iteration you should use filter and map instead of the forEach
Something like this:
var filteredEmployees = employees.filter(function(){
// your filter here
}).map(...);
You can separate the callback in diferent functions to make your code more simple. Or you can separate the filter and the map in different declarations.
var filteredEmployees = employees.filter(function(){
// your filter here
});
var mapEmployees = filteredEmployees.map(...);
Maybe this code isn't that you are looking for, but for sure do your code more clear and easy to manage.
Post a Comment for "How Can I Remove Employees From The Array That Have Enddate Property Higher Than Current Selected Month?"