javascript 开关内的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22277447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
indexOf within Switch
提问by Leaf
I have a Javascript-based bot for a Xat chatroom which also acts as an AI. I've recently decided to redo the AI part of it due to it becoming an absolutely massive chain of else if
statements, becoming nearly impossible to work with.
我有一个基于 Javascript 的机器人,用于 Xat 聊天室,它也充当 AI。我最近决定重做其中的 AI 部分,因为它变成了一个绝对庞大的else if
语句链,几乎无法使用。
I did some research and came up with a new idea of how to handle responses. I'll give you the code segment first:
我做了一些研究,并提出了如何处理响应的新想法。我先给你代码段:
function msgSwitch(id,msgRes) {
var botResponse = [];
switch (msgRes) {
case (msgRes.indexOf("hi") !=-1):
botResponse.push("HELLO. ");
case (msgRes.indexOf("how are you") !=-1):
botResponse.push("I AM FINE. ")
case (msgRes.indexOf("do you like pie") !=-1):
botResponse.push("I CAN'T EAT. THANKS, ASSHAT. ")
default:
respond (botResponse);
spamCount(id);
break;
}
}
The idea here is to check msgRes
(the user's input) and see how many cases it matches. Then for each match, it'll push
the response into the botResponse
array, then at the end, it'll reply with all the messages in that array.
这里的想法是检查msgRes
(用户的输入)并查看它匹配的案例数。然后对于每个匹配项,它会将push
响应放入botResponse
数组中,然后在最后,它将使用该数组中的所有消息进行回复。
Example
例子
User Msg: Hi! How are you?
用户留言: Hi! How are you?
msgRes: hi how are you
消息资源: hi how are you
Bot Matches:
机器人匹配:
hi
> pushes HELLO.
to array
hi
> 推HELLO.
送到数组
how are you
> pushes I AM FINE.
to array
how are you
> 推I AM FINE.
送到数组
Bot Responds:HELLO. I AM FINE.
机器人回复:HELLO. I AM FINE.
This in turn saves me the trouble of having to write an if
for each possible combination.
这反过来又省去了我必须if
为每个可能的组合编写一个的麻烦。
However, after looking into it some more, I'm not sure if it's possible use indexOf
inside of a switch. Does anyone know of a way around this or have a better idea for handling responses in the same manner?
但是,在进一步研究之后,我不确定是否可以indexOf
在开关内部使用。有没有人知道解决这个问题的方法或有更好的想法以相同的方式处理响应?
EDIT:
编辑:
To Avoid the XY Problem (To clarify my problem)
避免XY问题(澄清我的问题)
I need a clean alternative to using a massive chain of else if
statements. There are going to be hundreds of word segments that the bot will respond to. Without the ability for it to keep searching for matches, I'd have to write a new else if
for every combination.
我需要一个干净的替代方案来使用大量的else if
语句链。机器人将响应数百个词段。如果它无法继续搜索匹配项,我将不得不else if
为每个组合编写一个新的。
I'm hoping for a way to have it scan through every statement for a match, then combine the response for each match together into a single string.
我希望有一种方法让它扫描每个匹配项的语句,然后将每个匹配项的响应组合成一个字符串。
EDIT 2: I should also add that this is being ran on Tampermonkey and not a website.
编辑 2:我还应该补充一点,这是在 Tampermonkey 而不是网站上运行的。
采纳答案by ailveen
My two cents for the gist of what you're trying to do:
我的两分钱是你想要做的事情的要点:
function msgSwitch(id, msgRes) {
var seed = {'hi': 'HELLO. ', 'how are you': 'I AM FINE'};
var botResponse = [];
for (var key in seed) {
if (msgRes.indexOf(key) !== -1) {
botResponse.push(seed[key]);
}
}
}
In my opinion it is easier to change this program as you only have to edit the seed if you have more responses in the future. You can even stash the seed on some json file and read it (via ajax) so the program does not need to be changed if there are additional messages.
在我看来,更改此程序更容易,因为如果将来有更多响应,您只需编辑种子。您甚至可以将种子存储在某个 json 文件中并读取它(通过 ajax),因此如果有其他消息,则不需要更改程序。
回答by dandavis
you just need to compare to true instead of msgRes (since cases use === comparison), and use break to prevent the annoying fall-though of the switch behavior:
您只需要与 true 而不是 msgRes 进行比较(因为情况使用 === 比较),并使用 break 来防止令人讨厌的切换行为失败:
function msgSwitch(id,msgRes) {
var botResponse = [];
switch (true) {
case (msgRes.indexOf("hi") !=-1):
botResponse.push("HELLO. "); break;
case (msgRes.indexOf("how are you") !=-1):
botResponse.push("I AM FINE. "); break;
case (msgRes.indexOf("do you like pie") !=-1):
botResponse.push("I CAN'T EAT. THANKS, ASSHAT. "); break;
default:
respond (botResponse);
spamCount(id);
break;
}
}
This is a perfectly valid logical forking pattern, known as an "overloaded switch". A lot of folks might not realize that each case:
is an expression, not just a value, so you could even put an IIFE in there if needed...
这是一个完全有效的逻辑分叉模式,称为“过载开关”。很多人可能没有意识到每一个case:
都是一个表达式,而不仅仅是一个值,所以如果需要,你甚至可以把一个 IIFE 放在那里......