从数组中获取日期,并从日历中禁用那些重复超过两次的日期



我想从数组中获得重复3次的日期,这样我就可以从日历中禁用这些日期。

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

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

Ok,更新我的答案


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

最新更新