【问题标题】:filter an array by property returns unfiltered array [closed]按属性过滤数组返回未过滤的数组[关闭]
【发布时间】:2021-05-28 11:23:26
【问题描述】:

我正在尝试通过这样的属性过滤一组对象

if (payment == Payment.CREDIT_CARD) {
  this.currenies.filter((currency: Currency) => currency.isFromEurope === true);
  console.log(this.currencies)
}

最初,货币数组有135个项目,过滤相同数量的项目后(其中至少30个有isFromEurope = true);

【问题讨论】:

  • 数据在哪里(currenies)?
  • filter 创建一个新数组,因此如果您想更新 this.currencies,您可以分配过滤器,例如this.currencies = this.currencies.filter(...)
  • 这能回答你的问题吗? Array.Filter not updating array

标签: javascript typescript filter


【解决方案1】:

来自documentation of Array.filter:

filter() 方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。

即它不会改变原始数组。

另外,你也有一个错字 (this.currenies)。

应该是这样的:

if (payment == Payment.CREDIT_CARD) {
  this.currencies = this.currencies.filter((currency: Currency) => currency.isFromEurope === true);
  console.log(this.currencies)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多