到目前为止,我如何在没有 "Present" 元素的数组中填写缺席的所有日期?
How do I fill absent on all days in an array till now which are not having the "Present" Element?
让我们假设下面是一个包含员工出勤数据的数组(使用Ajax从Mongo获取):
[{"_id":"5fcdcd49c3657d1e05b846f5","title":"Present","allDay":true,"start":"2020-11-20","display":"background","color":"#4caf50"},
{"_id":"5fcdcd74c3657d1e05b846f6","title":"Present","allDay":true,"start":"2020-12-07","display":"background","color":"#4caf50"},
{"_id":"5fcebc7653057438acc633a6","title":"Present","allDay":true,"start":"2020-12-08","display":"background","color":"#4caf50"},]
此数组由 JSON 格式的元素组成。
当读取数组时,我们发现在每个元素中,JSON 对象包含两个字段 - Start(提到日期)和 Title(提到员工是否在场)
现在,我们注意到在 (2020-11-20) 上有一个元素表明该员工在场。然后是下一个元素,说明该员工在 2020-12-07(大约 17 天后)在场。根据我的用例,我假设该员工已经缺勤 17 天。
现在,
这17天是-
- 2020-11-21
- 2020-11-22
- 2020-11-23
...
- 至 2020-12-06
我愿意:
$.ajax(
{
url: "/api/employees/markAttendence/Absent/:date" [We will fill the date obviously, I have left it as :date for illustration]
type: "GET"
}
);
员工缺勤的每一天。
我想用moment.js从今天减去天数,检查元素是否存在并计算出日期时刻。如果没有元素,我会发送上面的 Ajax 那个日期的请求。
我该怎么做?
我不知道你做了多少所以我们从数据处理开始。
根据给定的数据,我想将开始信息提取到一个名为 presentDate
的数组中。
var dataset = [{"_id":"5fcdcd49c3657d1e05b846f5","title":"Present","allDay":true,"start":"2020-11-20","display":"background","color":"#4caf50"},
{"_id":"5fcdcd74c3657d1e05b846f6","title":"Present","allDay":true,"start":"2020-12-07","display":"background","color":"#4caf50"},
{"_id":"5fcebc7653057438acc633a6","title":"Present","allDay":true,"start":"2020-12-08","display":"background","color":"#4caf50"},]
var presentDate = dataset.filter(data => data.title === "Present").map(data => data.start);
我尝试与 Moment.js
一起工作,并回顾了大约 30
天。查看 presentDate
中是否存在任何日期并跳过它们。
var today = moment(moment().format('YYYY-MM-DD'));
var lookBackDays = 30;
var NeedToQuery = [];
for(var index = 1; index <= lookBackDays; ++index) {
var checkDate = moment(today);
checkDate.subtract(index, 'days');
var dateString = checkDate.format('YYYY-MM-DD');
if(presentDate.indexOf(dateString) < 0)
{
// Here I store the date string into NeedToQuery
NeedToQuery.push(dateString);
}
}
最后你会有日期字符串来发送 Ajax 请求,祝你好运。
谢谢@ian00!我也找到了一个很好用的解决方案。
function markAbsentism() {
$.ajax({
url: "/******/******/*******/",
type: "GET",
}).done((result) => {
var presentDates = [],
excludedDates = [];
if (
moment(result[result.length - 1].start).diff(
moment(result[result.length - 2].start),
"days"
) == 1 &&
(result[result.length - 1].title == "Present" ||
result[result.length - 1].title == "Absent") &&
(result[result.length - 2].title == "Present" ||
result[result.length - 2].title == "Absent")
) {
console.log("IF_1 SUCCESS");
return;
} else {
console.log("IF_1 FAILED");
console.log("ELSE_1 EXEC STARTED");
for (let ctr = result.length - 1; ctr >= 0; ctr--) {
if (
result[ctr].title == "Present"
) {
if (presentDates.length <= 2) {
console.log("EMPLOYEE PRESENT");
presentDates.push(result[ctr].start);
excludedDates.push(result[ctr].start);
} else {
console.log("GOT 2 PRESENT DATES. BREAKING LOOP");
break;
}
} else if (
result[ctr].title == "Leave" ||
result[ctr].title == "Holiday" ||
result[ctr].title == "Half-Day"
) {
console.log("EMPLOYEE IN LEAVE");
excludedDates.push(result[ctr].start);
}
}
console.log(presentDates);
console.log(excludedDates);
}
console.log("Proceeding to For Loop");
for (
let i = 0;
i <= moment(presentDates[0]).diff(moment(presentDates[1]), "days");
i++
) {
if (
excludedDates.includes(
moment(presentDates[1]).add(i, "days").format("YYYY-MM-DD")
)
) {
console.log("EXCLUDED DATE CAUGHT CONTINUING");
continue;
} else {
console.log("SENDING AJAX REQUEST");
$.ajax({
url:
"/******/******/*******/" +
moment(presentDates[1]).add(i, "days").format("YYYY-MM-DD"),
type: "GET",
});
}
}
});
}
我会试一试你的,找到最有效的。
让我们假设下面是一个包含员工出勤数据的数组(使用Ajax从Mongo获取):
[{"_id":"5fcdcd49c3657d1e05b846f5","title":"Present","allDay":true,"start":"2020-11-20","display":"background","color":"#4caf50"},
{"_id":"5fcdcd74c3657d1e05b846f6","title":"Present","allDay":true,"start":"2020-12-07","display":"background","color":"#4caf50"},
{"_id":"5fcebc7653057438acc633a6","title":"Present","allDay":true,"start":"2020-12-08","display":"background","color":"#4caf50"},]
此数组由 JSON 格式的元素组成。 当读取数组时,我们发现在每个元素中,JSON 对象包含两个字段 - Start(提到日期)和 Title(提到员工是否在场)
现在,我们注意到在 (2020-11-20) 上有一个元素表明该员工在场。然后是下一个元素,说明该员工在 2020-12-07(大约 17 天后)在场。根据我的用例,我假设该员工已经缺勤 17 天。
现在, 这17天是-
- 2020-11-21
- 2020-11-22
- 2020-11-23 ...
- 至 2020-12-06
我愿意:
$.ajax(
{
url: "/api/employees/markAttendence/Absent/:date" [We will fill the date obviously, I have left it as :date for illustration]
type: "GET"
}
);
员工缺勤的每一天。
我想用moment.js从今天减去天数,检查元素是否存在并计算出日期时刻。如果没有元素,我会发送上面的 Ajax 那个日期的请求。
我该怎么做?
我不知道你做了多少所以我们从数据处理开始。
根据给定的数据,我想将开始信息提取到一个名为 presentDate
的数组中。
var dataset = [{"_id":"5fcdcd49c3657d1e05b846f5","title":"Present","allDay":true,"start":"2020-11-20","display":"background","color":"#4caf50"},
{"_id":"5fcdcd74c3657d1e05b846f6","title":"Present","allDay":true,"start":"2020-12-07","display":"background","color":"#4caf50"},
{"_id":"5fcebc7653057438acc633a6","title":"Present","allDay":true,"start":"2020-12-08","display":"background","color":"#4caf50"},]
var presentDate = dataset.filter(data => data.title === "Present").map(data => data.start);
我尝试与 Moment.js
一起工作,并回顾了大约 30
天。查看 presentDate
中是否存在任何日期并跳过它们。
var today = moment(moment().format('YYYY-MM-DD'));
var lookBackDays = 30;
var NeedToQuery = [];
for(var index = 1; index <= lookBackDays; ++index) {
var checkDate = moment(today);
checkDate.subtract(index, 'days');
var dateString = checkDate.format('YYYY-MM-DD');
if(presentDate.indexOf(dateString) < 0)
{
// Here I store the date string into NeedToQuery
NeedToQuery.push(dateString);
}
}
最后你会有日期字符串来发送 Ajax 请求,祝你好运。
谢谢@ian00!我也找到了一个很好用的解决方案。
function markAbsentism() {
$.ajax({
url: "/******/******/*******/",
type: "GET",
}).done((result) => {
var presentDates = [],
excludedDates = [];
if (
moment(result[result.length - 1].start).diff(
moment(result[result.length - 2].start),
"days"
) == 1 &&
(result[result.length - 1].title == "Present" ||
result[result.length - 1].title == "Absent") &&
(result[result.length - 2].title == "Present" ||
result[result.length - 2].title == "Absent")
) {
console.log("IF_1 SUCCESS");
return;
} else {
console.log("IF_1 FAILED");
console.log("ELSE_1 EXEC STARTED");
for (let ctr = result.length - 1; ctr >= 0; ctr--) {
if (
result[ctr].title == "Present"
) {
if (presentDates.length <= 2) {
console.log("EMPLOYEE PRESENT");
presentDates.push(result[ctr].start);
excludedDates.push(result[ctr].start);
} else {
console.log("GOT 2 PRESENT DATES. BREAKING LOOP");
break;
}
} else if (
result[ctr].title == "Leave" ||
result[ctr].title == "Holiday" ||
result[ctr].title == "Half-Day"
) {
console.log("EMPLOYEE IN LEAVE");
excludedDates.push(result[ctr].start);
}
}
console.log(presentDates);
console.log(excludedDates);
}
console.log("Proceeding to For Loop");
for (
let i = 0;
i <= moment(presentDates[0]).diff(moment(presentDates[1]), "days");
i++
) {
if (
excludedDates.includes(
moment(presentDates[1]).add(i, "days").format("YYYY-MM-DD")
)
) {
console.log("EXCLUDED DATE CAUGHT CONTINUING");
continue;
} else {
console.log("SENDING AJAX REQUEST");
$.ajax({
url:
"/******/******/*******/" +
moment(presentDates[1]).add(i, "days").format("YYYY-MM-DD"),
type: "GET",
});
}
}
});
}
我会试一试你的,找到最有效的。