代码之家  ›  专栏  ›  技术社区  ›  elisa

从二维数组中检索随机值

  •  0
  • elisa  · 技术社区  · 7 年前

    我希望有人能帮助我。我试图创造一种多语言的Alexa技能。

    我的主文件中有一些语言常量,如Alexa提供的示例代码所示:

    const languageString = {
    'en': {
        'translation': {
            'QUESTIONS': questions['QUESTIONS_EN_US'],
            'GAME_NAME': 'My Quiz', // Be sure to change this for your skill.
            'HELP_MESSAGE': 'I will ask you %s multiple choice questions. Respond with the number of the answer. ' +
                'For example, say one, two, three, or four. To start a new game at any time, say, start game. ',
    

    一切都很好,所以我现在想添加一些替代字符串,这样游戏就不那么无聊了。所以我创建了一个正确的。js文件,并在我的文档中定义如下:

    const correct = require('./correct');
    

    此文件如下所示:

    'use strict';
    
    module.exports = {
    
        CORRECT_EN_GB: [
            "Booya", "All righty", "Bam", "Bazinga", "Bingo", "Boom", "Bravo", "Cha Ching", "Cheers", "Dynomite",
            "Hip hip hooray", "Hurrah", "Hurray", "Huzzah", "Oh dear.  Just kidding.  Hurray", "Kaboom", "Kaching", "Oh snap", "Phew",
            "Righto", "Way to go", "Well done", "Whee", "Woo hoo", "Yay", "Wowza", "Yowsa"
        ],
        CORRECT_EN_US: [
            "Booya", "All righty", "Bam", "Bazinga", "Bingo", "Boom", "Bravo", "Cha Ching", "Cheers", "Dynomite",
            "Hip hip hooray", "Hurrah", "Hurray", "Huzzah", "Oh dear.  Just kidding.  Hurray", "Kaboom", "Kaching", "Oh snap", "Phew",
            "Righto", "Way to go", "Well done", "Whee", "Woo hoo", "Yay", "Wowza", "Yowsa"
        ],
        CORRECT_DE_DE: [
            "Aber hallo", "Bazinga", "Bingo", "Bravo", "Donnerwetter",
            "en garde", "hipp hipp hurra", "hurra", "japp", "jawohl", "jo", "juhu", "na sieh mal einer an", "Stimmt",
            "Super", "Supi", "tada", "türlich", "yay"
        ],
    };
    

    现在,我想在我的语言字符串中检索列表的随机值,并尝试了以下代码:

                'ANSWER_CORRECT_MESSAGE': '<say-as interpret-as="interjection">' + correct['CORRECT_EN_US'][Math.floor(Math.random() * correct['CORRECT_EN_US'].length)] + '</say-as><break time="1s"/> your reply is correct.<break time="2s"/>',
    

    谁能帮我找到正确的密码?非常感谢。

    酶联免疫吸附试验

    1 回复  |  直到 7 年前
        1
  •  2
  •   Adrian    7 年前

    这不是多维数组,您拥有的是一个具有包含数组的属性的对象。如果你发布的代码是你的代码,那么它很好,你只有一个额外的逗号( , )模块内部。导出对象。

    检查我的 JSFiddle 例如