循环遍历雪花数组

2022-01-10 00:00:00 discord node.js javascript discord.js

我正在寻找一种从该集合中获取数据的方法.

I'm looking for a way to get data from this Collection.

数据如下:

  '0000000' => GuildMember {
  guild:
   Guild {
     members: [Object],
     id: '000000',
     name: 'Zombie',
     _rawVoiceStates: [Object] },
  user:
   User {
     id: '0000000',
     username: 'Orc',
  _roles: [ '0000' ],
  nickname: 'Orc',
  joinedTimestamp: 00000,
  lastMessageID: null },

  '0000000' => GuildMember {
  guild:
   Guild {
     members: [Object],
     id: '000000',
     name: 'Zombie',
     _rawVoiceStates: [Object] },
  user:
   User {
     id: '0000001',
     username: 'Orc1',
  _roles: [ '0000' ],
  nickname: 'Orc',
  joinedTimestamp: 00000,
  lastMessageID: null },
  _array: null,
  _keyArray: null }

我当前的循环是:

var user;
for(var u in test.members){
   user = test.members[u];
    console.log("["+u+"] "+user.username);
}

目前它会返回一个 TypeError: Cannot read property 'user' of null

我最初认为这个数据是一个数组,但它不是根据 Discord.js 文档,但我仍然不确定如何从集合中提取用户名数据.

I originally thought this the data was an array, but it's not according to the Discord.js docs, but I'm still not sure how to pull the username data from the collection.

任何帮助都会有所帮助.

Any help would be helpful.

推荐答案

现在我查看了 discord.js API,我认为你要做的事情是这样的(假设 test 是你的公会对象):

Now i looked into the discord.js API and i think what u got todo is something like this (assuming test is your guild object):

test.members.forEach(function(guildMember, guildMemberId) {
   console.log(guildMemberId, guildMember.user.username);
})

如果这不起作用,请尝试以下方法:

If that doesn't work try along the lines of:

var membersArray = test.members.array();

for(var guildMemberId in membersArray) {
   console.log(guildMemberId, membersArray[guildMemberId].user.username);
}

相关文章