简体   繁体   English

比较两个arrays的对象,判断第一个数组的日期是否在第二个数组Javascript的指定范围内

[英]Compare two arrays of objects and find out if the date from the first array is within the specified range in the second array in Javascript

let array1 = [
    {
        start_date: 2022-12-01,
        end_date: 2022-12-07,
        status: ready,
    },
    {
        start_date: 2022-12-04,
        end_date: 2022-12-10,
        status: ready,
    },
    
   {
        start_date: 2022-12-8,
        end_date: 2022-12-14,
        status: ready,
    },
    {
        start_date: 2022-12-11,
        end_date: 2022-12-17,
        status: ready,
    },
   {
        start_date: 2022-12-15,
        end_date: 2022-12-21,
        status: overlap,
    },

   {
        start_date: 2022-12-18,
        end_date: 2022-12-24,
        status: overlap,
    },

   {
        start_date: 2022-12-22,
        end_date: 2022-12-28,
        status: overlap,
    },
    {
        start_date: 2022-12-25,
        end_date: 2022-12-31,
        status: overlap,
    },
    
    {
        start_date: 2022-12-18,
        end_date: 2022-12-31,
        status: match,
    },

    
];




let array2 = [
    {
        start_date: 2022-12-18,
        end_date: 2022-12-31
    }
];

I have two array of objects and我有两个对象数组和

  1. would like to compare start date of array 1 against array 2 to see if array 1 start date is within the range of array 2 start date and end date想要将数组 1 的开始日期与数组 2 进行比较,以查看数组 1 的开始日期是否在数组 2 的开始日期和结束日期的范围内

then also compare end date of array 1 against array 2 to see if array 1 end date is within the range of array 2 start date and end date然后还将数组 1 的结束日期与数组 2 进行比较,以查看数组 1 的结束日期是否在数组 2 的开始日期和结束日期的范围内

if both the condition above are met then validate if array 1 status is overlap.如果满足上述两个条件,则验证数组 1 状态是否重叠。

eg: Dec 15-21 (array 1 - start date -end date ) Dec 18-31 (from array 2 start date -end date)例如:Dec 15-21(数组 1 - 开始日期 - 结束日期) Dec 18-31(从数组 2 开始日期 - 结束日期)

  1. if array1 start date is outside the range of array 2 start and end date, then validate array 1 status is ready.如果数组 1 的开始日期在数组 2 的开始和结束日期范围之外,则验证数组 1 的状态是否已就绪。 and if array1 end date is outside the range of array 2 start and end date, then validate array 1 status is ready.如果数组 1 结束日期超出数组 2 开始和结束日期的范围,则验证数组 1 状态已准备就绪。

eg: Dec 01-07 (array 1 - start date -end date ) Dec 18-31 (from array 2 start date -end date)例如:Dec 01-07(数组 1 - 开始日期 - 结束日期) Dec 18-31(从数组 2 开始日期 - 结束日期)

  1. and if array 1 start date matches array 2 start date and array 1 end date matches array 2 end date then validate array 1 status is match,如果数组 1 的开始日期与数组 2 的开始日期匹配,并且数组 1 的结束日期与数组 2 的结束日期匹配,则验证数组 1 的状态是否匹配,

eg: Dec 18-31 (array 1 - start date -end date ) Dec 18-31 (from array 2 start date -end date)例如:Dec 18-31(数组 1 - 开始日期 - 结束日期) Dec 18-31(从数组 2 开始日期 - 结束日期)

Can you please advise how to achieve this using javascript.您能否告知如何使用 javascript 实现此目的。

can you please advise if something like this is possible or any better solution, if there is a way to validate the status is either overlap/ready/available within every block or make every block return the status instead of just boolean value - true/false如果有一种方法可以验证每个块中的状态是否重叠/就绪/可用,或者使每个块返回状态而不仅仅是 boolean 值 - 真/假

let compareTwoArrayOfObjects = (
    array1,
    array2
) => {
    return (
        array2.every((element_2) =>
            array1.every(          
                (element_1) => {

                    if(element_2.start_date < element_1.start_date && element_1.start_date < element_2.end_date) return OVERLAP;
                    if(element_2.start_date < element_1.end_date && element_1.end_date < element_2.end_date) return OVERLAP;
                    
                     
>! if(element_2.start_date == element_1.start_date && element_1.start_date == element_2.end_date) return MATCH;
>! 
>! 
>! if(element_2.start_date > element_1.start_date && element_1.start_date > element_2.end_date) return ready;
>! if(element_2.start_date > element_1.end_date && element_1.end_date > element_2.end_date) return ready;
>!                 }
            )
        )
    );
};

This should do the trick, the first if is just comparing if the time is the same, the second if statement is a condition when any of the first array date is within range of the second array or if any of the dates on the second array is within the range of the current selected element of the first array, the last if is a condition when the first date is outside the range of second date:这应该可以解决问题,第一个 if 只是比较时间是否相同,第二个 if 语句是第一个数组日期中的任何一个在第二个数组的范围内或者第二个数组中的任何日期是否存在的条件在第一个数组的当前选中元素的范围内,最后一个if是第一个日期在第二个日期范围之外的条件:

 let array1 = [{ start_date: "2022-12-01", end_date: "2022-12-07", }, { start_date: "2022-12-04", end_date: "2022-12-10", }, { start_date: "2022-12-8", end_date: "2022-12-14", }, { start_date: "2022-12-11", end_date: "2022-12-17", }, { start_date: "2022-12-15", end_date: "2022-12-21", }, { start_date: "2022-12-18", end_date: "2022-12-24", }, { start_date: "2022-12-22", end_date: "2022-12-28", }, { start_date: "2022-12-25", end_date: "2022-12-31", }, { start_date: "2022-12-18", end_date: "2022-12-31", }, ]; let array2 = [{ start_date: "2022-12-18", end_date: "2022-12-31" }]; function compareTwoArrayOfObjects(array1, array2) { const start2 = new Date(array2[0].start_date); const end2 = new Date(array2[0].end_date); return array1.map(elm_arr1 => { const start1 = new Date(elm_arr1.start_date); const end1 = new Date(elm_arr1.end_date); let status; if (start1.getTime() === start2.getTime() && end1.getTime() === end2.getTime()) { status = "match"; } else if ((start1 > start2 && start1 < end2) || (end1 > start2 && end1 < end2) || (start2 > start1 && start2 < end1) || (end2 > start1 && end2 < end1)) { status = "overlap"; } else if ((start1 < start2 && end1 < start2) || (start1 > end2 && end1 > end2)) { status = "ready"; } return {...elm_arr1, status, }; }); } console.log(compareTwoArrayOfObjects(array1, array2));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两个具有对象的 arrays 并从第一个数组中删除重复项 - Compare two arrays having objects and remove duplicates from first array 比较两个数组并从第二个数组中不包含的第一个数组中获取值 - compare two arrays and take values ​from the first array not contained in the second array 如何根据日期范围过滤掉对象数组中的数组? - How to filter out array within array of objects based on date range? Angular - 比较两个 arrays 具有相同数量的对象但具有不同的值,并返回与第二个数组的差异 - Angular - Compare two arrays with same number of objects but with different values and return the difference from second array Javascript比较两个不同大小的数组,并返回不在第二个数组中的项目 - Javascript Compare two arrays with different sizes and return the items that are not in second array 比较两个数组并更新Javascript中的第二个数组元素 - Compare two arrays and update second array element in Javascript 从JavaScript中的两个数组生成对象数组 - Generate array of objects from two arrays in JavaScript 比较两个 Arrays 对象,如果 Javascript 中的值为真,则返回一个新数组 - Compare Two Arrays Of Objects and return a new array if value is true in Javascript Javascript-比较两个数组并将缺少的项添加到第一个数组中 - Javascript - Compare two arrays and add missing items into first array 如何比较两个 arrays 对象并向第二个数组中的对象添加新键 - How can I compare two arrays of objects and add new key to an objects in the second array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM