从二维数组

时间:2017-10-30 10:55:11

标签: javascript arrays node.js alexa alexa-skill

我希望有人可以帮助我。我尝试创建一种多语言的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"/>',

但这不起作用,说实话,我不知道如何从(多维?)数组中检索随机值。

有人能引导我找到正确的代码吗?谢谢!

Elisa的

1 个答案:

答案 0 :(得分:2)

这不是一个多维数组,你所拥有的是一个包含数组属性的对象。如果您发布的代码是您的代码,那么它很好,您在module.exports对象中只有一个额外的逗号(,)。

例如,检查我的JSFiddle