下面是我的函数,dotkoy变量:iterator.format('DD-MM-YYYY') == props.activity[0] || iterator.format('DD-MM-YYYY') == props.activity[2]
我不喜欢,props.activity是数组,我想要props.activity自动增加到数组长度,我想检查iterator.format('DD-MM-YYYY') == props.activity[0]...props.activity[props.acticity.length]
总结props.activity数组中有日期,我想用iterator.format('DD-MM-YYYY')加上时间和常数函数(如loadsh时间和常数方法)逐一检查日期。感谢您的任何建议
Calculate = (props) => {
const constant = x => () => x;
const times = (n, iterator) => {
let accum = Array(Math.max(0, n));
for (let i = 0; i < n; i++) accum[i] = iterator.call();
return accum;
};
let result = [];
const currentMonth = props.focus.month();
let iterator = moment(props.focus);
let i = 0;
let h = 0;
let control = []
while (iterator.month() === currentMonth) {
if (iterator.weekday() === 0 || result.length === 0) {
result.push(times(7, constant({})));
}
let week = result[result.length - 1];
week[iterator.weekday()] = {
date: iterator.date(),
dotkoy: iterator.format('DD-MM-YYYY') == props.activity[0] || iterator.format('DD-MM-YYYY') == props.activity[2],
selected: props.selected && iterator.isSame(props.selected, 'day'),
today: iterator.isSame(moment(), 'day'),
haftasonumu: iterator.format('ddd') == "Cts" || iterator.format('ddd') == "Paz",
};
iterator.add(1, 'day');
}
return result;
};
答案 0 :(得分:2)
只需使用Array#some
。这将对照您提供的回调检查数组的每个成员,如果至少一个成员通过了检查,则返回true
;如果没有成员通过,则返回false
:
dotkoy: props.activity.some(activity => iterator.format('DD-MM-YYYY') == activity)
如果要避免每次迭代都进行线性扫描,则可以为prop.activity
的所有成员预先计算查找并进行检查。在这种情况下,最简单的选择是Set
:
const lookupDates = new Set(props.activity);
/* ... */
dotkoy: lookupDates.has(iterator.format('DD-MM-YYYY')),