使用重复键过滤映射结果
filter map result with duplicate keys
我有一个命令处理程序,它使用一个映射,其中包含从用户命令文件夹和通用通信文件夹分配的命令。用户可以添加自己的命令。地图过滤器使用文件内的名称进行过滤,这也是调用它们的方式。如果有多个同名的,如何进行比较? Afaik 地图无法做到这一点,担心我必须全部重做。
起初我试图为每个用户分配自己的地图
client.User1.get(command, argument);
client.User2.get(command argument);
这真的是正确的做法吗?我正在使用 .JSON 和启用的命令名称进行初始比较。下面的一些代码,你可能会说我不是那么有经验。
const assignedChannels = client.opts.channels.slice(0);
assignedChannels.unshift('#basiccommands');
const rootForCommands = "./commands";
client.commandCall = new Map();
for (const channelspecificDir of assignedChannels) {
const commandFiles = fs.readdirSync(`${rootForCommands}/${channelspecificDir}`);
for (const file of commandFiles) {
const commandCollection = require(`${rootForCommands}/${channelspecificDir}/${file}`);
//sets a new item in the collection.
//Key of map as command name and the value the function to send message and argument true or false
client.commandCall.set(commandCollection.name, commandCollection);
const currentProfile = JSON.parse(fs.readFileSync(`./userProfiles/${channel}.json`, 'utf8'));
if (!currentProfile.all_commands.includes(commandPre)){
return console.log("Command does not exist");
}
try {
client.commandCall.get(commandPre, argU).execute(channel, argU);
} catch (error) {
console.log('command broke');
console.error(error);
}
我以前的方法是将用户特定文件夹和通用命令文件夹分配给地图,并在每次迭代结束时更改地图对象的名称。但由于某些原因,这不起作用。
实际上,您可以使用符号作为地图的键
const map = new Map()
map.set(Symbol(‘the same name’), value1)
map.set(Symbol(‘the same name’), value2)
您可以为 Map 的键使用任何值,甚至对象和函数
map.set({}, value3)
map.set({}, value4)
在所有前面的表达式中,我们为 Map 的键获得了不同的值(链接)。
顺便说一句。注意 const some = require(‘some_file_name’)
。
require()
缓存文件名的值。因此,如果您的文件可能会在 运行 时间内更新,并且您需要阅读这些更新,则不应使用“require()”。它只适用于导入模块和静态值(例如配置)
我有一个命令处理程序,它使用一个映射,其中包含从用户命令文件夹和通用通信文件夹分配的命令。用户可以添加自己的命令。地图过滤器使用文件内的名称进行过滤,这也是调用它们的方式。如果有多个同名的,如何进行比较? Afaik 地图无法做到这一点,担心我必须全部重做。
起初我试图为每个用户分配自己的地图
client.User1.get(command, argument);
client.User2.get(command argument);
这真的是正确的做法吗?我正在使用 .JSON 和启用的命令名称进行初始比较。下面的一些代码,你可能会说我不是那么有经验。
const assignedChannels = client.opts.channels.slice(0);
assignedChannels.unshift('#basiccommands');
const rootForCommands = "./commands";
client.commandCall = new Map();
for (const channelspecificDir of assignedChannels) {
const commandFiles = fs.readdirSync(`${rootForCommands}/${channelspecificDir}`);
for (const file of commandFiles) {
const commandCollection = require(`${rootForCommands}/${channelspecificDir}/${file}`);
//sets a new item in the collection.
//Key of map as command name and the value the function to send message and argument true or false
client.commandCall.set(commandCollection.name, commandCollection);
const currentProfile = JSON.parse(fs.readFileSync(`./userProfiles/${channel}.json`, 'utf8'));
if (!currentProfile.all_commands.includes(commandPre)){
return console.log("Command does not exist");
}
try {
client.commandCall.get(commandPre, argU).execute(channel, argU);
} catch (error) {
console.log('command broke');
console.error(error);
}
我以前的方法是将用户特定文件夹和通用命令文件夹分配给地图,并在每次迭代结束时更改地图对象的名称。但由于某些原因,这不起作用。
实际上,您可以使用符号作为地图的键
const map = new Map()
map.set(Symbol(‘the same name’), value1)
map.set(Symbol(‘the same name’), value2)
您可以为 Map 的键使用任何值,甚至对象和函数
map.set({}, value3)
map.set({}, value4)
在所有前面的表达式中,我们为 Map 的键获得了不同的值(链接)。
顺便说一句。注意 const some = require(‘some_file_name’)
。
require()
缓存文件名的值。因此,如果您的文件可能会在 运行 时间内更新,并且您需要阅读这些更新,则不应使用“require()”。它只适用于导入模块和静态值(例如配置)