如何在没有循环的情况下获取Javascript对象的所有键

时间:2017-08-02 13:52:07

标签: javascript node.js azure botframework

我有一个Object,我希望能够搜索该软件的名称。对于此示例,我们将使用" Adob​​e Acrobat"。

const SoftwareDict {

"Actuate": {
              "Standard": "Retire",
           },
"Adobe Acrobat": {
              "Adobe Acrobat Pro DC": "Mainstream",
              "Adobe Acrobat Professional 7.0.8": "Retire",
              "Adobe Acrobat Professional 9.0.0":"Retire",
              "Adobe Acrobat Professional X":"Retire",
              "Adobe Acrobat Professional XI":"Contain",
              "Adobe Acrobat Reader 7.0.8":"Retire",
              "Adobe Acrobat Reader 9.0.0":"Retire",
              "Adobe Acrobat Reader DC":"Mainstream",
              "Adobe Acrobat Reader X":"Retire",
              "Adobe Acrobat Reader XI":"Contain",
           },
};

当我知道我要搜索的密钥时,它可以工作:

session.send(SoftwareDict["Adobe Acrobat"]["Adobe Acrobat Pro DC"]);

我遇到的问题是当我尝试使用循环从我的对象获取键名并将值存储在"选项"阵列。我尝试过for循环和forEach方法,但都没有工作。当我在Bot Framework Channel Emulator中离线运行时,循环工作完美。我目前正在Azure门户上运行以使用实体识别器,因此我需要它在此位置工作。

在此Bot Framework门户上运行循环是否存在问题。这是我第一次在这里使用它们。

bot.dialog('LifecycleWaterfallDialog', function (session, args) {
//Match Lifecycle to correct software using waterfall
        //Find Software Name
        var choices = [];
        session.send("Started");
        var softwareEntity = builder.EntityRecognizer.findEntity(args.intent.entities, 'Software');
        //Get List of Versions for that software
        if (softwareEntity) {

        session.send(SoftwareDict["Adobe Acrobat"]["Adobe Acrobat Pro DC"]); //This works & returns "Mainstream" 

        var versions = SoftwareDict["Adobe Acrobat"];
        versions.forEach(function(item){
            choices.push(item);
            session.send(item); //DOES NOT WORK
        });
        session.send(choices[0]); //DOES NOT WORK 

        //Outputs 
        session.send(SoftwareDict[softwareEntity.entity][choices[0]]); //testing first software version
        session.endDialog();
        } else {
            session.send("sorry");
            session.endDialog();
        }

}).triggerAction({
    matches: 'LifecycleStatus'
});

1 个答案:

答案 0 :(得分:0)

for..in将为您枚举对象属性

for (prop in versions) {
    choices.push(prop);
    session.send(prop);
}

或者如评论中已经提到的那样 - Object.keys会返回一个您可以迭代的键列表,例如

Object.keys(versions).forEach(...)