JavaScript满足条件移除数组元素及日期对比状态验证问题
JavaScript日期对比与状态验证实现需求
日期对比与状态验证规则
- 对比两个对象数组
array1与array2:- 若
array1元素的开始、结束日期均在array2某元素的日期范围内,验证该array1元素的status为"overlap"; - 若
array1元素的开始、结束日期均在array2某元素的日期范围外,验证其status为"ready"; - 若
array1元素与array2某元素的开始、结束日期完全匹配,验证其status为"match";
目前已实现同月份日期匹配对比逻辑。
- 若
- 筛选出
array1中与array2无对应月份的元素组成新数组array3,并验证array3中所有元素的status为"ready"; - 移除
array1中满足上述任一条件的元素,返回新数组。
当前实现代码
对比函数代码
function compareTwoArrayOfObjects(array1, array2) { array2.map(element => { const start2 = new Date(element.start_date); const end2 = new Date(element.end_date); const start2Month = start2.getMonth(); const end2Month = end2.getFullYear(); console.log("####################################") return array1.map(elm_arr1 => { const start1 = new Date(elm_arr1.start_date); const end1 = new Date(elm_arr1.end_date); let status; const start1Month = start1.getMonth(); const end1Month = end1.getFullYear(); if (start1.getTime() === start2.getTime() && end1.getTime() === end2.getTime() && ((start1Month === start2Month) || (end1Month === start2Month))) { console.log("**************") status = "match"; console.log(start1, end1) console.log(start2, end2) console.log(start1Month, start2Month) console.log(elm_arr1.status) //expect(elm_arr1.status).toMatch("match"); console.log("**************") } else if (((start1Month === start2Month) || (end1Month === start2Month)) && ((start1 > start2 && start1 < end2) || (end1 > start2 && end1 < end2) || (start2 > start1 && start2 < end1) || (end2 > start1 && end2 < end1))) { status = "overlap"; console.log("**************") console.log(start1, end1) console.log(start2, end2) console.log(start1Month, start2Month) console.log(elm_arr1.status) // expect(elm_arr1.status).toMatch("overlap"); console.log("**************") } else if (((start1Month === start2Month) || (end1Month === start2Month)) && ((start1 < start2 && end1 < start2) || (start1 > end2 && end1 > end2))) { status = "ready"; console.log("**************") console.log(start1, end1) console.log(start2, end2) console.log(start1Month, start2Month) console.log(elm_arr1.status) //expect(elm_arr1.status).toMatch("ready"); console.log("**************") } }); }); } const result = compareTwoArrayOfObjects(array1, array2);
测试数据代码
<script> let array1 = [{ start_date: "Dec 01 2022", end_date: "Dec 07 2022", status: "ready" }, { start_date: "Dec 04 2022", end_date: "Dec 10 2022", status: "ready" }, { start_date: "Dec 15 2022", end_date: "Dec 21 2022", status: "overlap" }, { start_date: "Dec 18 2022", end_date: "Dec 24 2022", status: "overlap" }, { start_date: "Dec 12 2022", end_date: "Dec 28 2022", status: "overlap" }, { start_date: "Dec 25 2022", end_date: "Dec 31 2022", status: "overlap" }, { start_date: "Nov 22 2022", end_date: "Nov 28 2022", status: "match" }, { start_date: "Nov 01 2022", end_date: "Nov 07 2022", status: "ready" }, { start_date: "Jan 01 2022", end_date: "Jan 07 2022", status: "ready" }, { start_date: "Feb 16 20226", end_date: "Feb 22 2022", status: "ready" }, ]; let array2 = [{ start_date: "Dec 18 2022", end_date: "Dec 31 2022" }, { start_date: "Nov 22 2022", end_date: "Nov 28 2022" }, ]; </script>
内容的提问来源于stack exchange,提问作者Banu
相关产品推荐
相关产品推荐

