我正在研究Alexa Skill lambda,并创建了小的代码片段来说明我的问题:

exports.handler = function(event, context)
{
    //CREATE A MODEL
    var model1 = {"endpoints":[ ]};

    //CREATE ANOTHER MODEL
    var model2 = {"endpointid": "stuff"};

    //CREATE THE RESULT

    var result = model1;
    model2.endpointid="switch1";
    result.endpoints.push(model2);

    model2.endpointid="switch2";
    result.endpoints.push(model2);

    var  json = JSON.stringify(result);
    context.succeed({json});
};


响应:

{
  "json": "{\"endpoints\":[{\"endpointid\":\"switch2\"},{\"endpointid\":\"switch2\"}]}"
}


问题:为什么我得到两个相同的endpointid

最佳答案

这行得通,但我觉得并不那么优雅:

exports.handler = function(event, context)
{
    //CREATE A MODEL
    var model1 = {"endpoints":[ ]};

    //CREATE ANOTHER MODEL
    var model2 = {"endpointid": "stuff"};

    //CREATE THE RESULT

    model1.endpoints.push(model2);
    model1.endpoints.push(model2);
    model1.endpoints[0]="switch1";
    model1.endpoints[1]="switch2";

     var  json = JSON.stringify(model1);
   context.succeed({json});


响应:“ json”:“ {\” endpoints \“:[\” switch1 \“,\” switch2 \“]}”

关于node.js - 使用PUSH时不了解node.js结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50190436/

10-14 06:13