【问题标题】:Filtering for activity with certain property过滤具有特定属性的活动
【发布时间】:2022-01-10 16:39:42
【问题描述】:

在 discordjs 中,我正在尝试读取用户的存在,然后执行“如果声明的存在之一(例如,用户可以同时拥有 Spotify 和游戏)在其属性中具有 Spotify 的名称,做一点事”。我的代码是:

let uset = msgMention.presence.activities
if (uset.filter(game => game.name == 'Spotify')){
     //something
}

由于多个游戏返回数组中的多个项目(见下文)

[
  Activity {
    name: 'Spotify',
    type: 'LISTENING',
    url: null,
    details: null,
    state: 'NO',
    applicationID: 'NO',
    timestamps: no,
    party: null,
    assets: RichPresenceAssets {
      largeText: null,
      smallText: null,
      largeImage: 'no',
      smallImage: null
    },
    syncID: no,
    flags: ActivityFlags { bitfield: 1 },
    emoji: null,
    createdTimestamp: no
  },
  Activity {
    name: 'Visual Studio Code',
    type: 'PLAYING',
    url: null,
    details: 'Editing league.js',
    state: 'Workspace: Vibin-Tachanks',
    applicationID: '383226320970055681',
    timestamps: { start: 2021-12-04T18:35:34.918Z, end: null },
    party: null,
    assets: RichPresenceAssets {
      largeText: 'Editing a JAVASCRIPT file',
      smallText: 'Visual Studio Code',
      largeImage: '808841241142755358',
      smallImage: '565945770067623946'
    },
    syncID: undefined,
    flags: ActivityFlags { bitfield: 0 },
    emoji: null,
    createdTimestamp: 1638645018501
  }
]

我怎样才能使这个 if 语句过滤掉这个返回的数组并只保留 Spotify,然后访问所述过滤项目的名称(或任何属性)。是的,我确实可以检测到与意图一起使用的存在,所以我知道我已经做到了这一点,它只是用 if 过滤(也不是意味着我不想显示其中的内容)

【问题讨论】:

    标签: javascript arrays discord discord.js


    【解决方案1】:

    您可以只使用在变量中使用的这个过滤器,并作为 if 语句的条件,您可以检查它是否不同于空数组“[]”。

    这样做,您可以使用变量中的值(如果有的话)。

    【讨论】:

    • 我根据你说的做了这个,但它似乎不起作用,它总是触发var spotTest = mp.filter(game => game.name == 'Spotify') if (spotTest != undefined)
    • 它返回一个空数组而不是未定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 2014-05-30
    • 2014-08-19
    相关资源
    最近更新 更多