Home > database >  Get dates from array and disable those dates from calendar that repeat
Get dates from array and disable those dates from calendar that repeat

Time:06-15

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();

CodePudding user response:

function disbaleDate() {

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);
    }

disbaleDate();

CodePudding user response:

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())

CodePudding user response:

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)
  • Related