我有一个日期数组,我想在其中过滤掉特定的日期。 这是我以前的解决方案,效果很好:
var available = dates.filter(function(e) {
return (
e.getDay() != 0 && // sunday
e.getDay() != 6 && // saturday
e.getDay() != 2 && // tuesday
e.getDay() != 3 && // wednesday
);
});
现在,我想让它动态化。所以我得到了一个这样的数组:
var unavailable = [0, 2, 3, 6]
然后我尝试像这样过滤掉那些日子:
unavailable.forEach(function(x){
available = dates.filter(function(e, index){
return e.getDay() != x;
});
});
不过这似乎不起作用。我做错了什么,我怎样才能让它发挥作用?提前致谢。
最佳答案
无需使用 forEach
使用 filter
和 includes
var unavailable = [0, 2, 3, 6]
var dates = ['1-1-2019', '1-2-2019', '1-3-2019', '1-4-2019',' 1-5-2019', '1-6-2019', '1-7-2019', '1-8-2019',' 1-9-2019'];
workingDays = dates.filter(e => {
return !unavailable.includes(new Date(e).getDate())})
console.log(workingDays)
关于javascript - 使用 forEach 过滤数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55041198/