If/else 用于语音选项设置

我正在尝试根据使用的是“文本”还是“ssml”来替换一些配置选项


我需要对文本进行完全匹配还是使用 a!=来始终确保完全匹配?


if textOption = "text" {

    const request = {

        input: { text: mytextvariable },

    }

    else {

        const request = {

            input: { ssml: mytextvariable },

        }

    }

}


慕田峪7331174
浏览 156回答 3
3回答

慕尼黑5688855

上述语句将始终执行,if因为您使用了=而不是==或===let request; // the scope should be outside the blocksif(textOption == "text") {    request = {        input: { text: mytextvariable },    }} else {    request = {        input: { ssml: mytextvariable },    }}

偶然的你

您需要括号和双/三等号来对值进行匹配。if (textOption === "text") {一种简短的方法是采用带有检查和替代方案的条件运算符。var textOption = 'text',    mytextvariable = 'foo',    request = {        input: textOption === "text"            ? { text: mytextvariable }            : { ssml: mytextvariable }    };console.log(request);

慕桂英546537

var textOption = 'text',    mytextvariable = 'foo',    request = {        input: { [textOption]: mytextvariable }    };这种方式属性名称基于textOption变量值。如果您的变量可以有多个值(不仅是文本和 ssml),您可以改用表达式request = {    input: { [textOption === 'text' ? 'text' : 'ssml']: mytextvariable }};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript