简体   繁体   中英

Return random word from a dictionary object with Javascript

I'm creating a Japanese text game, しりとり Wiki , where the PC gives a word and the user must give a word the first character matches the last character of the PC's word and vice versa. GitHub Repo

The part I am having trouble with is how I can return only a random word from a dictionary of words and having it starting with the determined character . In the this case あ

The dictionary is structured like so:

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

The way I would check if the User's word is valid would be

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

Could I make an array of all the keys within Dictionary["あ"] then do something like

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

Here's some code to get an random suboject and code to get a subObject which parent object is a determined string.

FIDDLE

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM