亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無論如何將數組中的對象定義為數字

無論如何將數組中的對象定義為數字

繁花不似錦 2022-07-15 09:31:49
我正在制作一個 discord.js 瑣事命令。但我不明白。到目前為止一切正常,我想為答案制作一個轉換器。喜歡:答案將是一個數字,而不是一個完整的答案。我到處搜索,但我找不到正確的答案。編碼:module.exports.run = async (client, message, args) => {        request.get("https://opentdb.com/api.php?amount=1&type=multiple", (err, response, body) => {        let res = JSON.parse(body)        let results = res.results[0]        let answers = results.incorrect_answers;        answers.push(results.correct_answer) // Pushes the correct answers in the incorrect answers array.        // Entity decode loop        var i;        for (let i = 0; i < answers.length; i++) {            entities.decode(answers[i])          }             shuffle(answers) // Shuffles the answers in a random order.'        console.log(answers)        let embed = new Discord.MessageEmbed()            .setAuthor(`${message.author.username} trivia vraag`, message.author.displayAvatarURL())            .setTitle(entities.decode(results.question))            .setDescription(`1) ${answers[0]} \n 2) ${answers[1]} \n 3) ${answers[2]} \n4) ${answers[3]}`)            .addField("Prijs", `\n ja wat Corona bacteri?n.`, true)            .addField(`Difficulty`, `\n ${results.difficulty}`, true)            .addField(`Categorie`, `\n ${results.category}`, true)                        message.channel.send(embed)            // console.log(results.question)            // console.log(results.correct_answer)    })}function shuffle(array) {    var currentIndex = array.length, temporaryValue, randomIndex;    while (0 !== currentIndex) {        randomIndex = Math.floor(Math.random() * currentIndex);        currentIndex -= 1;        temporaryValue = array[currentIndex];        array[currentIndex] = array[randomIndex];        array[randomIndex] = temporaryValue;    }    return array;}如果有人可以幫助我,請我需要它哈哈如果你不明白,請查看我添加的圖片。我收到的 Discord 消息,我想將完整答案轉換為數字。親切的問候,
查看完整描述

1 回答

?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

我有點困惑,但我想我明白了,

answers.findIndex(f => f === results.correct_answer);

這將基于 0 索引,因此添加 1。


查看完整回答
反對 回復 2022-07-15
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號