从二维数组中检索随机值

Retrieve a random value from a twodimensional array

希望有人能帮助我。我尝试创建多语言 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. ',

一切正常所以我现在想添加一些备用字符串,这样游戏就不那么无聊了。所以我创建了一个 correct.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"/>',

但这行不通,老实说,我不知道如何从(多维?)数组中检索随机值。

任何人都可以引导我找到正确的代码吗?谢谢!

酶联免疫吸附

这不是一个多维数组,您拥有的是一个对象,其属性包含数组。如果您发布的代码是您的代码,那么没关系,您只是在 module.exports 对象中多了一个逗号 (,)。

例如检查我的JSFiddle