简体   繁体   English

使用 Alexa-SDK 如何让 this.emit() 方法返回 JSON 数组中的多个对象?

[英]Using the Alexa-SDK how can I get this.emit() method to return multiple objects in a JSON array?

I'm trying to get multiple account id's from a JSON array using the emit() method.我正在尝试使用 emit() 方法从 JSON 数组中获取多个帐户 ID。 The emit method is defined from the Alexa-SDK.发射方法是从 Alexa-SDK 定义的。 The problem is that its emitting only one of the values and not all.问题是它只发出一个值而不是全部。 How can I get the emit()method to return both object values in a JSON array?我怎样才能让emit()方法返回一个JSON数组中的两个对象值?

Here is my code snippet:这是我的代码片段:

'use strict';

const Alexa = require('alexa-sdk');

const handlers = {
  'LaunchRequest': function() {
    this.emit(':tell', 'sure');
    this.emit('getEmployeeInfoIntent');
  },
  'getEmployeeInfoIntent': function() {
    var empinfoData = {
    "employees": [{
        "account_id": 8675309
      },{
        "account_id": 54321
      }]
    };
    for (var i in empinfoData) {
      var employeeInfo = empinfoData[i].account_id;
      this.emit(':tell', 'The accounts available are id number' + employeeInfo);
      //return only 8675309 but I want 8675309 and 54321
    };
  }
}; ///end of handler

exports.handler = (event, context) => {
  const alexa = Alexa.handler(event, context);
  alexa.APP_ID = APP_ID;
  alexa.registerHandlers(handlers);
  alexa.execute();
};

 var objtwo = { "employees": [{ "account_id": 8675309 },{ "account_id": 54321 }] } // mythis variable is defined globally var mythis = this; function getJSONArray() { //var reqresponse = this.responseText; //var objtwo = JSON.parse(reqresponse); var empinfoData = objtwo.employees; var allIds = []; for (var i of empinfoData) { var employeeInfo = i["account_id"]; allIds.push(employeeInfo); }; return allIds; }; console.log(getJSONArray());

Save the emploeeInfo's into an Array and emit the array after the loop.将 emploeeInfo 保存到一个数组中,并在循环后发出该数组。

暂无
暂无

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

相关问题 当用户在this.emit(“:ask”,speech)之后什么都没输入到Alexa时,我该如何解决? - How can I account for when the user inputs nothing to Alexa after this.emit(“:ask”, speech)? 我如何使用alexa-sdk在我的alexa技能中使用http? - How would I go about using http in my alexa skill using alexa-sdk? 如何将对象数组转换为JSON(并返回)? - How can I convert an array of objects into JSON (and return it)? 将AngularJS与嵌套在数组中的数组中的JSON对象一起使用,如何将数据获取到html中? - Using AngularJS with JSON objects in arrays nested within an array, how can I get the data into the html? 如何使用Javascript或Jquery将JSON对象包装在数组中? - How can I wrap JSON objects in an array using Javascript or Jquery? 从 json 文件读取时,如何返回对象而不是数组内的对象? - How can I return objects instead of objects inside an array when reading from json files? 如果我使用传播语法,如何完全返回内部嵌套对象的数组? - How can I completely return the array with nested array of objects inside if I am using spread syntax? 给定一个JSON对象数组,如何基于嵌套值获取JSON对象X? - Given an array of JSON objects, how can I get JSON object X based on a nested value? 如何使用POST方法在Django中读取多个JSON对象 - How do i read multiple json objects in django using POST method 如何返回属性与数组匹配的对象数组? - How can I return an array of objects whose property matches an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM