【问题标题】:Looping through a JSON array and push the values into an array variable using node.js循环遍历 JSON 数组并使用 node.js 将值推送到数组变量中
【发布时间】:2021-10-04 22:56:47
【问题描述】:

我正在尝试使用 node.js 在 Dialogflow 中创建一个聊天机器人。训练短语将来自 JSON 文件。

示例 JSON:

{
"Training_Phrases": ["How can I help you?", "How can I assist you today?"]
}

训练短语被添加到创建的意图上,但两个短语合并在一行中,而不是两个单独的短语。 这是我拥有的当前代码。 inputPhrases obj 是 JSON 文件:

          async function createIntent(inputPhrases)  { 
          
                const displayName = ('Intent: ' + intentName);

                 //create training phrases 
                const trainingPhrases_Parts = [inputPhrases];
                const trainingPhrases = [];

                  trainingPhrases_Parts.forEach(Phrases_Part => {
                   const part = {
                     text: Phrases_Part,
                   };

                    // Here we create a new training phrase for each provided part.
                   const trainingPhrase_con = {
                     parts: [part],
                    };

                    console.log(part);
                   trainingPhrases.push(trainingPhrase_con);

                  });

                //CONSTRUCT THE INTENT
                const intent = {
                   displayName: displayName,
                   trainingPhrases: trainingPhrases,
                   messages: [message], 
                   };
         

【问题讨论】:

    标签: javascript node.js arrays loops dialogflow-es


    【解决方案1】:
    const emptyArray = [];
    
    Training_Phrased.forEach((text)=>{emptyArray.push(text)})
    

    同样从您当前的代码来看,您想要实现的不是清除,而是此时您将整个对象推入数组,而不是实际存储数组的对象“部分”的属性。

    trainingPhrases.push(trainingPhrase_con);
    

    这应该是 trainingPhrases.push(trainingPhrase_con.parts); 如果您尝试将这些部分存储在 trainingPhrases 数组中。

    【讨论】:

      猜你喜欢
      • 2020-09-11
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 2022-01-19
      • 2013-02-12
      • 1970-01-01
      相关资源
      最近更新 更多