猿问

如何检查输入的单词是否等于数组中的任何字符串

我正在尝试制作输入字段(onChange带有道具的方法)。当我输入特定问题 ( string) 时,我会得到答案。我设法为单个字符串做到这一点,但我想多次传递一个字符串数组而不是一个字符串。


const newContent = (props) => {


let questions = '';

   if (props.question === "let") {

questions = <p> answer here </p>

   } else if (props.question === "var") {

questions = <p> answer here </p>

   } else if (props.question === "const") {

questions = <p> answer here </p>

   }


let nextQuestion = ["let", "var", "const"];

   if (props.question === nextQuestion) {

nextQuestion = <p> answer here </p>

   }

}


白猪掌柜的
浏览 91回答 2
2回答

慕工程0101907

这是你想要的?const anwers =&nbsp;&nbsp; &nbsp; { let:&nbsp; &nbsp;'<p> answer here for let </p>'&nbsp; &nbsp; , var:&nbsp; &nbsp;'<p> answer here for var </p>'&nbsp; &nbsp; , const: '<p> answer here for const </p>'&nbsp; &nbsp; }const newContent = props => anwers[props] ?&nbsp; anwers[props] : 'bad value';console.log( newContent('var'))

凤凰求蛊

像 ["let", "var", "const"].includes(props.question)
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答