我正在运行一个sql查询,该查询返回一个列days
,该列由以下类似值1111100
组成。这里每个比特表示每一天,例如返回值中的第一比特'1'
是星期一,第六比特'0'
是星期六。现在我想根据这些返回的值执行一些操作。例如,如果星期一是'1'
,我想在星期一触发一个操作,如果星期二是'0'
,我不必在星期一执行任何操作。NodeJ中可以使用什么方法来实现这一要求
// Jobs
let monJob = () => { console.log('Monday') };
let tueJob = () => { console.log('Tuesday') };
let wedJob = () => { console.log('Wednesday') };
let thuJob = () => { console.log('Thursday') };
let friJob = () => { console.log('Friday') };
let satJob = () => { console.log('Saturday') };
let sunJob = () => { console.log('Sunday') };
// Inputs
let bitDays = '0111100';
//let days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
// Verify block
let jobs = [monJob, tueJob, wedJob, thuJob, friJob, satJob, sunJob]
bitDays.split('').forEach((bit, index) => {
if (bit == '1')
jobs[index]();
});
我认为Node.js中没有这个功能,但你可以:
const mask = "1111100";
const dayOfWeekMap = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
];
for (let idx in mask) {
if (mask[idx] === "1") {
console.log(`Do something on ${dayOfWeekMap[idx]}`);
}
}