【问题标题】:How to check if typed word is equal to any string from array如何检查输入的单词是否等于数组中的任何字符串
【发布时间】:2020-04-01 02:24:47
【问题描述】:

我正在尝试制作输入字段(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>
   }
}

【问题讨论】:

标签: javascript arrays reactjs string


【解决方案1】:

这是你要找的吗?

const anwers = 
    { let:   '<p> answer here for let </p>'
    , var:   '<p> answer here for var </p>'
    , const: '<p> answer here for const </p>'
    }

const newContent = props => anwers[props] ?  anwers[props] : 'bad value';

console.log( newContent('var'))

【讨论】:

  • 感谢我挖掘对象版本,但无法将其连接到我的输入字段。我设法使它与 {newContent(props.question)} 一起工作,感谢您的帮助
【解决方案2】:

类似 ["let", "var", "const"].includes(props.question)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-22
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2021-07-25
    • 2023-03-11
    相关资源
    最近更新 更多