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

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

對象的返回值

對象的返回值

哆啦的時光機 2023-05-25 15:36:32
我正在嘗試為我的 discord.js 機器人創建一個積分系統(您可以使用命令進行驗證以檢查您是否完成了挑戰,如果是,機器人會給您積分)。我找不到檢查值是否正確的方法。該命令如下所示:!verify <flag-name> <value>這是我的代碼:const Discord = require('discord.js');const flag = { flag1: { value: 'test', points: 20 }, flag2: { value: 'test2', points: 30 },};module.exports.run = async (client, message) => { let args = message.content.slice(4).split(' '); var keys = Object.keys(flag); keys.forEach((key) => {  if (key == args[2]) {   var str = JSON.stringify(keys);   var result = JSON.parse(str);   console.log(result['value']);  } });};問題是result['value']總是返回undefined,即使我知道標志名稱和值是有效的。
查看完整描述

1 回答

?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

Object.entries()您可以使用和檢查值是否正確Array.prototype.find()

const flag = {

?flag1: { value: 'test', points: 20 },

?flag2: { value: 'test2', points: 30 },

};


module.exports.run = async (client, message) => {

?// get the challenge and value using destructuring

?let [cmd, challenge, value] = message.content.slice(4).split(' ');


?// find the entry with both names matching the names given

?const result = Object.entries(flag).find(

? ([flag, data]) => flag === challenge && data.value === value

?);


?// provide an error if nothing is found

?if (!result)

? return message.channel.send('Not a valid value and/or flag');


?// do something with the points

?console.log(result[1].points)

};

JSFiddle


查看完整回答
反對 回復 2023-05-25
  • 1 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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