【问题标题】:Call function if variable does not exist in a filter如果过滤器中不存在变量,则调用函数
【发布时间】:2019-08-17 08:28:38
【问题描述】:

我正在对工作正常的视图中显示的数据进行过滤。我在屏幕顶部放置了一个过滤器栏,用户可以在其中过滤记录。我想要实现的是当用户输入的变量在记录中找不到时,应该调用一个函数

 filterProducts(ev) {
    this.productService.list = this.reOrderList;
    const val = ev.target.value;
    if (val && val.trim() !== '') {
      this.productService.list = this.reOrderList.filter((item) => {
        return (item.name.toLowerCase().indexOf(val.toLowerCase()) > -1);
      });
    } else {
     // value doesn't exist console.log('call another function')
   }
  }

【问题讨论】:

  • 你试过检查过滤结果的长度吗?

标签: javascript arrays angular object


【解决方案1】:

过滤完成后检查数组中是否有剩余项:

if (this.productService.list.length) {
  // The user's query was found in the array
} else {
  // The user's query was not found in the array
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 2018-12-05
    • 2013-06-24
    • 2021-09-23
    • 2018-08-31
    相关资源
    最近更新 更多