数据来自 Google Sheets API,因此 employees.stratdate
、employees.enddate
等。
我需要能够仅显示过去在选定月份工作的员工。如果他们不再工作,但曾经工作过,则应列出他们,如果他们在选定月份之前停止工作,则不应列出。
有 2 张: a) 一个有两列年和一个月 -->
2011 年 3 月 -> 报告 b) 另一个在包含日期上编造为:
03-24-2011 -> 员工 [开始日期和结束日期格式如下]
这就是为什么那种愚蠢的格式化尝试......
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);
}
});
提前致谢:)
最佳答案
如何简单地添加在第一个循环中对应的员工:
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 - 如何从数组中删除 endDate 属性高于当前所选月份的员工?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49606016/