Javascript 如何从数组中删除endDate属性高于当前选定月份的员工?

Javascript 如何从数组中删除endDate属性高于当前选定月份的员工?,javascript,arrays,reactjs,google-sheets-api,Javascript,Arrays,Reactjs,Google Sheets Api,数据来自谷歌表单API,因此employees.stratdate,employees.enddate等 我需要能够只显示过去在所选月份工作的员工。如果他们不再工作,但已经习惯了工作,那么他们应该被列出,如果他们在所选月份之前停止工作,那么他们不应该被列出 共有2张: a) 其中一个有两列,一年一个月--> 2011年3月->报告 b) 另一个在包含日期为: 2011年3月24日->员工[开始日期和结束日期的格式如下] 这就是为什么那个愚蠢的格式化尝试 const {employees, rep

数据来自谷歌表单API,因此
employees.stratdate
employees.enddate

我需要能够只显示过去在所选月份工作的员工。如果他们不再工作,但已经习惯了工作,那么他们应该被列出,如果他们在所选月份之前停止工作,那么他们不应该被列出

共有2张: a) 其中一个有两列,一年一个月-->

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

2011年3月24日->员工[开始日期和结束日期的格式如下]

这就是为什么那个愚蠢的格式化尝试

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);
                }
            });
const{employees,reports}=this.props;
const monthformatted=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=()=>{
让extraFormatting=[];
for(设i=0;i{
让我们测试一下;
返回测试=(值[1]+'-'+值[0])
});

让employeeArr=employees.map(项=>{ 返回时刻(item.startdate)。格式('M-YYYY') }) 让newArr=Array.from(finalreportsformating).map(item=>{ 退货项目 }) 设testArr=[]; 对于(变量i=0;i{ if(testArr.some(项=>时刻(emp.startdate).format('M-YYYY')==项)&& testArr.some(item=>(moment(emp.enddate).format('M-YYYY')>item))){ 结果:推送(emp); } });

提前感谢:)

只需添加在第一个循环中对应的员工如何:

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);
    }
});
我希望这能帮助你

javascript数组中有一个方法可以帮助您。作为映射,它迭代数组中的每个项,退出数组方法

在上一次迭代中,应该使用过滤器和映射,而不是forEach

大概是这样的:

var filteredEmployees = employees.filter(function(){
   // your filter here
}).map(...);
您可以在不同的函数中分离回调,使代码更简单。也可以在不同的声明中分离过滤器和映射

var filteredEmployees = employees.filter(function(){
   // your filter here
});
var mapEmployees = filteredEmployees.map(...);

也许这个代码不是你要找的,但确实要让你的代码更清晰、更易于管理。

什么是
employeeArr
,它与
employeer
?什么是
newArr
?你能给你的代码提供更多的上下文吗?让employeeArr=employees.map(item=>{return moment(item.startdate).format('M-YYYY')})这只是出于格式化的原因。不同的列顺序、斜杠、数字、字母等…工作表是一团乱让newArr=Array.from(finalreportsformating).map(项=>{return item})仍然可以从书本上找到所有的工作人员!让我发布整个代码,这样可能会更清楚!我现在就编辑这个问题,尽可能清楚…谢谢你!!!没有错误,因为你帮了我上一个重要的答案。你对员工所做的,我也需要对报告进行处理,然后以一种只有employe才能比较的方式进行比较显示了在特定月份工作的es->>这是来自报告表,在“最终报告格式”中格式化,最终在newArr中格式化…这有什么意义吗?我太累了,伙计。我需要完成这项工作。谢谢!你已经帮到我了。