简体   繁体   English

在Node.js中的变量内部获取价值

[英]Getting value inside of variable in Node.js

I have two speeches to select from for outputting in Alexa SDK eg 我有两个演讲可供选择,例如在Alexa SDK中输出

const HI = 'Hi Trump';
const HELLO = 'Hello Trump';

I store the variable name in an array: 我将变量名存储在数组中:

const speech = [HI, HELLO];

I process and get the index number of the speech I want to output. 我处理并获取要输出的语音的索引号。 Now the problem is how to output the speech - 'Hello' in the following line of codes: 现在的问题是如何在以下代码行中输出语音-“ Hello”:

return handlerInput.responseBuilder
   .speak($speech[1]);
},

You can output the array index as speech response by using template strings. 您可以使用模板字符串将数组索引输出为语音响应。 Try the following: 请尝试以下操作:

 return handlerInput.responseBuilder .speak(`${speech[1]}`); }, 

I see what you are trying to achieve, but JS works differently. 我看到了您要实现的目标,但是JS的工作方式有所不同。 You do not need a pointer with a $ symbol. 您不需要带有$符号的指针。

Can you try the following? 您可以尝试以下吗?

const HI = 'Hi Trump';
const HELLO = 'Hello Trump';
const textToSpeak = [HI, HELLO];

textToSpeak.forEach((text) => {
    handlerInput.responseBuilder.speak(text);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM