Need help building a method that returns the earliest date value
P粉521013123
P粉521013123 2024-04-04 19:51:19
0
1
1382

I'm refactoring a method for a legacy codebase I took over. This method accepts an object of predicted dates. When the forecast date is within 30, 40, or 60 days, the getTime() method should be used to convert, returning the earliest time.

Currently returns the earliest date of the object it receives, the problem is that it only compares with 2 dates, it returns a star date and only returns 1 of the 2 values ​​from the end date instead of comparing all 3 and Return the lowest .getTime() value

The following is the method to return the earliest date

const getEarliestRunout = (runout_dates = {}, dm1_type = '') => {
     try{
        const cur_year = new Date().getFullYear();

        const non_priority_sticker = dm1_type == cur_year + 1 || dm1_type == cur_year ? 'dm1' : 'dm2';
    
        return Object.entries(runout_dates).reduce((earliest,obj) => {            
            const key = obj[0].split('_')[0];

            const value = obj[1];
            
            if(value[`under_${env.STICKER_THRESH[0]}`] && key != non_priority_sticker) {
                return new Date(value[`under_${env.STICKER_THRESH[0]}`]).getTime() < earliest.val ? {val: new Date(value[`under_${env.STICKER_THRESH[0]}`]).getTime(), date: value[`under_${env.STICKER_THRESH[0]}`]} : {...earliest}
            }
            return earliest
        },{val: Infinity, date:''})
    }catch(e){
        console.error(`ERROR :: util.getEarliestRunout: ${e} - ${new Date()}`);
        return {val: Infinity, date: ''}
    }
}

The following are the variables used to call the method being used

const earliest_runout = getEarliestRunout({
 dm1_runouts: value.dm1_type == priority_sticker ? priorityRunouts : nonPriorityRunouts,
 dm2_runouts: value.dm2_type == priority_sticker ? priorityRunouts : nonPriorityRunouts,
 star_runouts: starRunouts
},value.dm1_type);

I expected this method to compare all dates, but it only compares 2. I'm pretty sure this is how I'm calling the method, but I'm not sure. I'm looking to resolve this issue further as I'm confused on how to obtain the results I'm looking for.

I'm sure I can rephrase this more clearly, but just to reiterate. This method accepts date values ​​(only 3 dates in my use case) and returns the earliest date using Math.min and getTime(). The result I receive is that it returns all date values ​​but only compares the asterisk value to one of dm1_runout or dm2_runout. It remains consistent across comparisons, returning only one or the other, but not both.

P粉521013123
P粉521013123

reply all(1)
P粉949267121

I solved my problem with the answer. I created an array of dates, ran each method through the Math.min method and pushed each value into the date array, then sorted the array to filter the NaNs with the empty string and return only the values ​​in Math.min Loop through each object's array matching the minimum value.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!