Tag: switch statement的

使用switch case更新集合中的多个文档

我在我的NodeJS应用程序中使用MongoDB本地驱动程序。 我在我的数据库中有一个需要更新的shifts集合。 在我的class次集合中的示例文档 { "_id" : ObjectId("588425105560bd2ba0065fa4"), "from" : ISODate("2017-01-23T03:20:00.000Z"), "to" : ISODate("2017-01-23T06:20:00.000Z"), "jobId" : ObjectId("586efda790541421b0432897"), "hourlyRate" : 15 } { "_id" : ObjectId("588425105560bd2ba0065fa5"), "from" : ISODate("2017-01-25T03:20:00.000Z"), "to" : ISODate("2017-01-25T06:20:00.000Z"), "jobId" : ObjectId("586efda790541421b0432897"), "hourlyRate" : 15 } 我需要做的是以下 – 更新符合条件的所有文档的hourlyRate : 匹配jobId(很简单) 设置hourlyRate = 20如果from是Weekday 设置hourlyRate = 25如果from星期六开始 设置hourlyRate = 30如果from星期天开始 我想尽可能在​​单个查询中完成。 我的解决scheme迄今为止 使用开关大小写,并使用date聚合函数中的$dayOfWeek确定datetypes。 但是,我无法将交换机与updateMany结合使用。 […]

使用.includes()时,Switch语句不起作用

client.on('chat', function(channel, userstate, message, self){ switch(message){ case message.includes(emotes[0]): numberOfEmotes[0]++; console.log("Emote0 has been used " + numberOfEmotes[0] + " time(s)"); break; case message.includes(emotes[1]): numberOfEmotes[1]++; console.log("Emote1 has been used " + numberOfEmotes[1] + " time(s)"); break; case message.includes(emotes[2]): numberOfEmotes[2]++; console.log("Emote2 has been used " + numberOfEmotes[2] + " time(s)"); break; case message.includes(emotes[3]): numberOfEmotes[3]++; console.log("Emote3 has been used […]

“跳字典”,转换语句,或如何做“跳字典”没有评估?

我喜欢包含键值的“跳字典”的概念,其中值是函数。 不过,我不确定我是否应该喜欢这个概念。 如果if-else if语句,我想replace一个长列表。 (我应该使用switch语句吗?) 有没有一种方法来实现“跳字典”而不使用eval? (我已经阅读了关于为什么eval不好的原因: 为什么使用JavaScript eval函数是一个坏主意? ) 使用eval的例子 function some_func(arg) { console.log('I am a some func') }; function find_my(arg) { console.log('we are looing for your ' + arg); }; var jump = { '1' : 'some_func()', '2' : 'find_my("cat")', '3' : 'find_my("dog")' } eval(jump['3']) eval(jump['2']) eval(jump['1']) 谢谢。

JS / node.js:大型开关块与function集合

我在Github上看到了这样的代码: switch (type) { case 'case1': return this.case1(); case 'case2': return this.case2(); case 'case3': return this.case3(); … default: return this.default(); } 它包含25个案例+默认情况。 我会用不同的方式把所有的function打包到一个对象中: var list = {}; list.case1 = function() { return /* code */; }; list.case2 = function() { return /* code */; }; list.case3 = function() { return /* code */; }; // […]