简体   繁体   中英

Get dates from array and disable those dates from calendar that repeat more than twice

I want to get dates from array which are repeated 3 times so I can disable those dates from calendar.

 function disbaleDate() { const arr = [ "1/6/2022", "12/6/2022", "4/6/2022", "6/6/2022", "1/6/2022", "1/6/2022", ]; const increment = []; for (let i = 1; i < arr.length; i++) { for (let j = 0; j < i; j++) { if (arr[j] === arr[i]) { increment.push(arr[i]); } } } console.log(increment); } disbaleDate();

function disableDate() {
    const arr = ["1/6/2022", "12/6/2022", "4/6/2022", "6/6/2022", "1/6/2022", "1/6/2022",];
       
    const backendData = ["12/12/2022", "12/6/2021", "14/6/2022", "16/6/2022", "1/6/2022", "11/6/2022",];
        
    const increment = [];

    if (backendData.length > 0) {
        for (let i = 0; i < backendData.length; i++) {
            if (arr.includes(backendData[i])) {
                increment.push(backendData[i]);
            }
        }
    }

    console.log(increment);
}
disableDate();

Ok, updating my answer


// reducer willproduce an object with the date as the key, and the amount repeated as the value
const countRepeated = arr.reduce((a, c) => {
    if (a[c]) {
        a[c] = a[c] + 1;
        return a;
    }
    
    a[c] = 1;
    return a;
}, {})

// will filter those whose values are greater than 2
return Object.keys(countRepeated).filter( date => date > 2)

 const disable = () => { const arr = [ "1/6/2022", "12/6/2022", "4/6/2022", "6/6/2022", "1/6/2022", "1/6/2022", ]; let data =[]; data = arr.filter((el, i) => i !== arr.indexOf(el) ) let result = data.filter((el,i) => i ===data.indexOf(el)) return result; } console.log(disable())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM