Home > Software design >  how to convert array to object for discordjs
how to convert array to object for discordjs

Time:10-16

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

        });
    }
}

CodePudding user response:

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

        });
    }
}

  • Related