搜索对象数组以匹配输入
Searching an object array to match an input
所以,我正在努力让一些东西发挥作用,但我碰壁了,我确信这只是因为我缺乏经验。
在下面的代码中,我试图获取用户为集换式纸牌游戏 (Bakugan) 输入的内容 (!activate [card name]) 并让系统从全小写字母中找到卡片,否空格格式。我有一个单独的 cardlist.js 文件,其中将卡片设置为对象。我尝试了很多不同的方法,但我一直收到“____ 不是一个函数”或者由于某种原因它只是无法在卡片列表文件中找到该项目。我知道我在尝试的几个小时里一直在围绕它跳舞。
const Discord = require("discord.js");
const botconfig = require("../botconfig.json");
const cardlist = require('../cardlist.js');
module.exports.run = async (bot, message, args) => {
//console.log("works");
//let aUser = `${message.author}`;
let aCard = message.content.slice(10);
oCard = Object.filter(function(cardlist){
return cardlist.name === aCard });
if (aCard === oCard) {
console.log('Cards match!');
} else {
console.log(`Cannot find ${aCard}`)
}
}
module.exports.help = {
name: "activate"
}
这是使用命令处理程序。我有许多其他命令可以正常使用它。在我试图让 aCard 和 oCard 匹配之前,代码一直在运行。我还尝试在卡片列表中搜索与用户输入的卡片名称相匹配的条目。下面是我对 cardlist.js
的布局
const cardlist = {
pyrushyperdragonoid: {
image: 'https://bakugan.wiki/wiki/images/thumb/3/3b/Hyper_Dragonoid_%28Pyrus_Card%29_265_RA_BB.png/250px-Hyper_Dragonoid_%28Pyrus_Card%29_265_RA_BB.png',
name: 'Pyrus Hyper Dragonoid',
faction: 'Pyrus',
energy: 1,
BPower: '400',
Type: 'Evo',
Damage: 6,
Effect: ':redfist: : +300 :Bicon: and +3:attackicon:'
},
dragosfury: {
name: 'Drago\'s Fury',
Energy: 2,
Type: 'Action',
Effect: '+4:attackicon:. Fury: If you have no cards in hand, +:doublestrike:'
}
}
因此,例如:1) 用户输入命令“!activate pyrushyperdragonoid”
2)我希望机器人自动剪掉输入中的“!activate”。 (完成没有错误)
3) 然后机器人应该获取该条目并搜索 cardlist.js 并检索该卡片列表的所有其他部分。
4) 我还没有在这段代码中完成,但我将使用 RichEmbed 来显示检索到的所有信息。
我希望这一切都有意义!感谢您提前提供的所有帮助。
Object.filter
不存在,即使存在:什么是 Object?
因为你有一个命令处理程序,你这里的代码只能从它接收 "arguments",即不是整个消息,而是 bang 命令之后的内容(这里是 !activate
) ,这将避免难以理解的 .slice(10)
。不管怎样
你显然不想匹配人类可读的名称("Pyrus Hyper Dragonoid"
),你想匹配键名(pyrushyperdragonoid
,最好不要有阅读障碍)。您可以像任何对象一样对它进行寻址,用括号表示键:
// aCard sounds like an object of the same nature as oCard, but it's just a string
var userInput = message.content.slice(10);
var oCard = cardlist[userInput];
// then you can test
if (oCard) {
console.log(`You legitimately chose to activate ${oCard.name}!`);
// here oCard is a sub-object of cardlist, such as { image: '...', name: '...', faction... }
} else {
console.log(`No mighty beast answered to the name ${userInput}!`);
// here oCard is undefined
}
所以,我正在努力让一些东西发挥作用,但我碰壁了,我确信这只是因为我缺乏经验。
在下面的代码中,我试图获取用户为集换式纸牌游戏 (Bakugan) 输入的内容 (!activate [card name]) 并让系统从全小写字母中找到卡片,否空格格式。我有一个单独的 cardlist.js 文件,其中将卡片设置为对象。我尝试了很多不同的方法,但我一直收到“____ 不是一个函数”或者由于某种原因它只是无法在卡片列表文件中找到该项目。我知道我在尝试的几个小时里一直在围绕它跳舞。
const Discord = require("discord.js");
const botconfig = require("../botconfig.json");
const cardlist = require('../cardlist.js');
module.exports.run = async (bot, message, args) => {
//console.log("works");
//let aUser = `${message.author}`;
let aCard = message.content.slice(10);
oCard = Object.filter(function(cardlist){
return cardlist.name === aCard });
if (aCard === oCard) {
console.log('Cards match!');
} else {
console.log(`Cannot find ${aCard}`)
}
}
module.exports.help = {
name: "activate"
}
这是使用命令处理程序。我有许多其他命令可以正常使用它。在我试图让 aCard 和 oCard 匹配之前,代码一直在运行。我还尝试在卡片列表中搜索与用户输入的卡片名称相匹配的条目。下面是我对 cardlist.js
的布局const cardlist = {
pyrushyperdragonoid: {
image: 'https://bakugan.wiki/wiki/images/thumb/3/3b/Hyper_Dragonoid_%28Pyrus_Card%29_265_RA_BB.png/250px-Hyper_Dragonoid_%28Pyrus_Card%29_265_RA_BB.png',
name: 'Pyrus Hyper Dragonoid',
faction: 'Pyrus',
energy: 1,
BPower: '400',
Type: 'Evo',
Damage: 6,
Effect: ':redfist: : +300 :Bicon: and +3:attackicon:'
},
dragosfury: {
name: 'Drago\'s Fury',
Energy: 2,
Type: 'Action',
Effect: '+4:attackicon:. Fury: If you have no cards in hand, +:doublestrike:'
}
}
因此,例如:1) 用户输入命令“!activate pyrushyperdragonoid” 2)我希望机器人自动剪掉输入中的“!activate”。 (完成没有错误) 3) 然后机器人应该获取该条目并搜索 cardlist.js 并检索该卡片列表的所有其他部分。 4) 我还没有在这段代码中完成,但我将使用 RichEmbed 来显示检索到的所有信息。
我希望这一切都有意义!感谢您提前提供的所有帮助。
Object.filter
不存在,即使存在:什么是 Object?
因为你有一个命令处理程序,你这里的代码只能从它接收 "arguments",即不是整个消息,而是 bang 命令之后的内容(这里是 !activate
) ,这将避免难以理解的 .slice(10)
。不管怎样
你显然不想匹配人类可读的名称("Pyrus Hyper Dragonoid"
),你想匹配键名(pyrushyperdragonoid
,最好不要有阅读障碍)。您可以像任何对象一样对它进行寻址,用括号表示键:
// aCard sounds like an object of the same nature as oCard, but it's just a string
var userInput = message.content.slice(10);
var oCard = cardlist[userInput];
// then you can test
if (oCard) {
console.log(`You legitimately chose to activate ${oCard.name}!`);
// here oCard is a sub-object of cardlist, such as { image: '...', name: '...', faction... }
} else {
console.log(`No mighty beast answered to the name ${userInput}!`);
// here oCard is undefined
}