如何遍历 enmap?

How to iterate through an enmap?

我正在尝试为我的 discord.js 机器人循环访问 enmap,我已经设法从单个条目设置和获取值,但我正在尝试设置一个命令将人们添加到像 DM 这样的关于次要重大更新的时事通讯。

if (args[0] === 'minor') {
  if (devlog.updates === 'minor') return message.channel.send('You are already recieving minor updates.').then(m => m.delete(5000))
  await client.devlog.set(userID, "yes", 'subscribed');
  await client.devlog.set(userID, "minor", 'updates');
  return message.channel.send('You will now recieve minor and major updates.').then(m => m.delete(5000))
}
if (args[0] === 'major') {
  if (devlog.updates === 'major') return message.channel.send('You are already recieving major updates.').then(m => m.delete(5000))
  await client.devlog.set(userID, "yes", 'subscribed');
  await client.devlog.set(userID, "major", 'updates');
  return message.channel.send('You will now recieve only major updates.').then(m => m.delete(5000))
}
if (!args[0]) {
  if (devlog.subscribed === 'yes') {
    await client.devlog.set(userID, "no", 'subscribed');
    await client.devlog.set(userID, "none", 'updates');
    return message.channel.send('You will stop recieving updates about RoboTurtle all together').then(m => m.delete(5000))
  }
  if (devlog.subscribed === 'no') {
    return message.channel.send(`Please choose wether you\'d like to recieve minor or major updates! (minor has both) **devlog minor/major**`).then(m => m.delete(10000))
  }
}

它有点用,但如果他们已经订阅了相同类型的更新,则不会触发消息,如果他们只订阅了!devlog,则意味着要么将他们设置为不接收更新,如果他们已经是,或者如果他们不是,则告诉他们在两者之间进行选择,但是它只会以任何一种方式发送最后一条消息。

我尝试设置我的 enmap 迭代,以便使用基于 .map 相关文档的 for...of 功能与所有订阅的人进行 DM(因为它们只是 "fancier" 地图)但无济于事,因为它们并没有真正显示不和谐风格的用例。

if (args[0] === 'minor') {
  for (let entry of client.devlog) {
    if (entry.updates === 'minor') {
      let user = client.users.get(entry)
      user.send(`**[Minor Update]\n${args.slice(1)}`)
    }
  }
}
if (args[0] === 'major') {
  for (let entry of client.devlog) {
    if (entry.subscribed === 'yes') {
      let user = client.users.get(entry)
      user.send(`**[Major Update!]\n${args.slice(1)}`)
    }
  }
}

如果有人想查看完整代码以更好地了解我在这里尝试做什么:https://pastebin.com/bCML6EQ5

因为它们只是普通 Map class 的扩展,我会用 Map.forEach():

遍历它们
let yourEnmap = new Enmap();
yourEnmap.set('user_id', 'your_values');

yourEnmap.forEach((value, key, map) => {
  ...
});

在你的情况下它会是这样的:

client.devlog.forEach((values, user_id) => {
  // you can check your subscription here, the use the user_id to send the DM
  client.users.get(user_id).send("Your message.");
});