如何从阵列中删除endDate属性高于当前选定月份的员工? [英] How can i remove employees from the array that have endDate property higher than current selected month?

查看:95
本文介绍了如何从阵列中删除endDate属性高于当前选定月份的员工?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据来自Google表格API,因此 employees.stratdate employees.enddate

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. If they don't work anymore, but used to work then, they should be listed, if they stopped working before the selected month, they shouldn't be listed.

有2张纸:
a)一个有两个列和一个月 - >

THERE ARE 2 SHEETS: a) ONE HAS TWO COLUMNS YEAR AND A MONTH -->

2011年3月 - >报告
b)另一个包含的日期为:

2011 March -> reports b) THE OTHER ONE HAS ON CONTAINING DATE FORATED AS:

03-24- 2011 - >员工[startdate&结束这样的结构]

这就是为什么这种愚蠢的格式化尝试......

That's why that silly attempt of formatting ...

const {employees, reports} = this.props;  

   const monthsFormated = reports.map(item => {return item.month});
   const yearsFormated = reports.map(item => {return item.year});

   const employeeStart = employees.map(item => {return item.startdate});
   const employeeEnd = employees.map(item => {return item.enddate});

   const monthNamesToNumbers = () => {
       let extraFormatting = [];
       for (let i=0; i<monthsFormated.length; i++) {
        extraFormatting.push(moment().month(monthsFormated[i]).format("M"));
       }
       return extraFormatting;
    }   

    // FROMATING DATES COMING FROM EMPLOYEES-TEMPLATE    
    let finalReportsFormatting = _.zip(yearsFormated, monthNamesToNumbers())
    .map((value) => {
    let test;
    return test = (value[1] + '-' + value[0])
    });


    let employeeArr = employees.map(item => {
        return moment(item.startdate).format('M-YYYY')
     })         

    let newArr = Array.from(finalReportsFormatting).map(item => {
        return item
    })

    let testArr = [];

    for (var i = 0; i < employeeArr.length; i++) {
        for (var j = 0; j < newArr.length; j++) {
            if (employeeArr[i] === newArr[j]) {
                testArr.push(newArr[j])
              }
            }    
        }

        const result = [];
            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);
                }
            });

感谢您提前:)

推荐答案

如何简单地添加第一个循环期间对应的员工:

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);
    }
});

这篇关于如何从阵列中删除endDate属性高于当前选定月份的员工?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆