0

我正在创建一个日文文本游戏,しりとり Wiki,其中 PC 给出一个单词,用户必须给出一个单词,第一个字符与 PC 单词的最后一个字符匹配,反之亦然。GitHub 回购

我遇到问题的部分是如何从单词词典中只返回一个随机单词并让它以确定的字符开头。在这种情况下あ</p>

字典的结构如下:

var dictionary = {
    "あ": {
        "あひる": {
            meaning: "Duck",
            kanji: "家鴨",
            sentenceJP: "アヒルに似てるの。",
            sentenceEN: "It looks like a duck."
        },
        "あなた": {
            meaning: "You",
            kanji: "貴方",
            sentenceJP: "あなたのお名前は?",
            sentenceEN: "What's your name?"
        }
    }
}

我检查用户的话是否有效的方法是

"あひる" in Dictionary["あ"]
//true

我可以制作一个包含所有键的数组Dictionary["あ"]然后执行类似的操作

var あWords = Object.keys(dictionary["あ"]);
var randomWord = あWords[Math.floor(Math.random() * あWords.length))];
4

1 回答 1

0

这是一些获取随机子对象的代码和获取父对象是确定字符串的子对象的代码。

小提琴

var myObject = {
    "Key1": {
        "SubKey1": {
            property: "Key1SubKey1"
        },
            "SubKey2": {
            property: "Key1SubKey2"
        }
    },
        "Key2": {
        "SubKey1": {
            property: "Key2SubKey1"
        },
            "SubKey2": {
            property: "Key2SubKey2"
        }
    }
}

    function getRandomSubObjectStartsWith(myObject, startsWith) {
        var count = 0;
        var obj = myObject[startsWith];
        for (var prop in obj) {
            // important check that this is objects own property 
            // not from prototype prop inherited
            if (obj.hasOwnProperty(prop)) {
                count++;
            }
        }

        var random = Math.floor(Math.random() * (count));
        count = 0;
        for (var prop in obj) {
            // important check that this is objects own property 
            // not from prototype prop inherited
            if (obj.hasOwnProperty(prop)) {
                if (count == random) {
                    return obj[prop];
                }
                count++;
            }
        }
    }

    function getRandomSubObject(myObject) {
        var count = 0;

        for (var key in myObject) {
            var obj = myObject[key];
            for (var prop in obj) {
                // important check that this is objects own property 
                // not from prototype prop inherited
                if (obj.hasOwnProperty(prop)) {
                    count++;
                }
            }
        }
        var random = Math.floor(Math.random() * (count));
        count = 0;
        for (var key in myObject) {
            var obj = myObject[key];
            for (var prop in obj) {
                // important check that this is objects own property 
                // not from prototype prop inherited
                if (obj.hasOwnProperty(prop)) {
                    if (count == random) {
                        return obj[prop];
                    }
                    count++;
                }
            }
        }
    }

    alert("Starts with 'Key2' random -- " + getRandomSubObjectStartsWith(myObject, "Key2").property);

    alert("Random -- " + getRandomSubObject(myObject).property);
于 2013-09-02T09:10:39.403 回答