Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用重复键筛选贴图结果_Javascript_Node.js_Map Function - Fatal编程技术网

Javascript 使用重复键筛选贴图结果

Javascript 使用重复键筛选贴图结果,javascript,node.js,map-function,Javascript,Node.js,Map Function,我有一个命令处理程序,它使用从用户命令文件夹和常规comm文件夹分配的命令映射。用户可以添加自己的命令。映射使用文件中的名称进行过滤,文件中的名称也是映射调用它们的方式。如果有多个名称相同,如何进行比较?Afaik maps无法做到这一点,担心我必须重做 起初,我试图为每个用户分配自己的地图 client.User1.get(command, argument); client.User2.get(command argument); 这真的是正确的方法吗?我使用带有启用命令名的.JSON

我有一个命令处理程序,它使用从用户命令文件夹和常规comm文件夹分配的命令映射。用户可以添加自己的命令。映射使用文件中的名称进行过滤,文件中的名称也是映射调用它们的方式。如果有多个名称相同,如何进行比较?Afaik maps无法做到这一点,担心我必须重做

起初,我试图为每个用户分配自己的地图

 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.set({}, value3)
map.set({}, value4)
在前面的所有表达式中,我们为贴图的键获取不同的值(链接)

顺便说一句,注意
constsome=require('some\u file\u name')

require()
缓存文件名的值。因此,如果您的文件可能在运行时更新,并且需要读取这些更新,则不应使用“require()”。它只适用于导入模块和静态值(例如,配置)

我已经读过关于这方面的文章,需要的是,我必须研究一下。我回家后会试试这些符号,让你知道。我在想也许我需要一个函数构造函数来生成映射对象。这样我就可以根据它获取命令的用户来命名每个地图。但是还没有找到这样命名变量的方法。客户端[stringLiteralHere]不起作用,它提供的只是空映射。为什么使用一个
符号
作为映射中的键而不存储在任何位置会有用?如果不将它保存到其他地方,您永远无法通过符号在地图中查找它,因此不会真正对地图数据结构有任何特殊用途。如果不打算保存密钥,还可以使用
数组
集合
。另外,这个建议如何帮助解决OP的实际问题也不清楚。此外,您不能使用
新符号(someString)
创建符号。你用
Symbol(someString)
@jfriend00和
new
这是一个打字错误。感谢您注意到。@jfriend00您不能在数据库中保存符号。但是你可以比较符号。TBH我们可以使用符号的情况并不多。通常我会避开它们,并使用其他方法。