Search code examples
javascriptdiscorddiscord.js

how to convert array to object for discordjs


How to convert an array to an object like {}
The result I receive in the code below is an array

currently the result is [ { result: result} ] but I want to convert it to { result:result }

I tried fromEntries, But it shows result: { undefined: undefined }

Here is the Code:

module.exports = {
    name: "wki",
    aliases: [],
    run: async (client, message, args, color) => {
        const query = args.join(" ")

        wikia.search(query).then(results => {
            console.log(results)
            const obj = Object.fromEntries(results);
            console.log(obj);

        });
    }
}

Solution

  • If the array only contains single element, you can just access it using index:

    module.exports = {
        name: "wki",
        aliases: [],
        run: async (client, message, args, color) => {
            const query = args.join(" ")
    
            wikia.search(query).then(results => {
                console.log(results)
                const obj = results[0];
                console.log(obj);
    
            });
        }
    }