【问题标题】:Use Filter function on an Object with numbers as property names [duplicate]在具有数字作为属性名称的对象上使用过滤器功能[重复]
【发布时间】:2021-06-29 17:45:47
【问题描述】:

我正在尝试根据某个道具从道具中过滤出一组项目。更具体地说,mode 属性。但是,它并没有过滤掉这些项目,并且我得到 error 类似 configs.filter is not a function 的东西。

我看不出我在这里做过滤器的方式有什么问题。我正在尝试删除道具中包含 null 模式道具的项目。

【问题讨论】:

  • botconfigs.filter 不是函数,因为botconfigs 是一个对象(正如我在您的图像上看到的那样),而.filter 仅存在于数组中。你可以试试Object.values(botconfigs).filter

标签: javascript arrays reactjs


【解决方案1】:

botConfigs 不是一个数组,而是一个以数字为属性的对象。

所以,你可以对Object.values进行过滤:

filterConfigsByMode = (configId) => {
  const { botConfigs, filterBotConfigStatus } = this.props;
  const filteredList = Object.values(botConfigs).filter(item => item.mode !== null);
  return filteredList;
}

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 2010-09-25
    • 2013-06-19
    • 2013-07-23
    相关资源
    最近更新 更多