【问题标题】:Vue JS: search filter on numbersVue JS:数字搜索过滤器
【发布时间】:2022-01-09 20:17:53
【问题描述】:

当我搜索字母时,我下面的代码运行良好,但是当我搜索像 flat_number 这样的数字时,它只会给我错误 flat.flat_number.toLowerCase is not a function

filteredList() {
  return this.Flats.filter((flat) => {
    return (
      //i tried commented code but it didn;t work
      //  this.Flats.filter(flat_number => String(flat_number).includes(this.search)) ||

      flat.buyer
      .toLowerCase()
      .includes(this.search.toLowerCase()) ||
      flat.flat_number //flat_number not working
      .toLowerCase()
      .includes(this.search.toLowerCase()) ||
      flat.city
      .toLowerCase()
      .includes(this.search.toLowerCase())
    );
  });
},

有什么帮助吗?

【问题讨论】:

  • 请分享this.Flats
  • 这是一个API数据@evolutionxbox
  • @LucaKiebel 我需要它作为搜索栏
  • 好吧,数字没有toLowerCase()方法
  • @LucaKiebel 我试过了,但没用this.Flats.filter(flat_number => String(flat_number).includes(this.search))

标签: javascript vue.js


【解决方案1】:

Number 没有 toLowerCase 函数。

如果您想将 flat 视为 String 类型,请在与搜索字符串进行比较之前进行转换。

filteredList() {
    return this.Flats.filter((flat) => {
        return (
            flat.buyer.toString()
                .toLowerCase()
                .includes(this.search.toLowerCase()) ||
            flat.flat_number.toString()
                .toLowerCase()
                .includes(this.search.toLowerCase()) ||
            flat.city.toString()
                .toLowerCase()
                .includes(this.search.toLowerCase())
        );
    });
},

【讨论】:

  • 谢谢你成功了......我还有一个问题,我的搜索栏渲染很慢你知道这是为什么吗?
  • 如果不查看项目的其余部分,将很难猜到。您可能想在 codereview 上为您的项目创建一个帖子以获得帮助:codereview.stackexchange.com
  • @sara97 话虽如此。该问题可能与数据量以及您检查匹配值的方式有关。 includes 可能会很昂贵,因为它正在扫描整个字符串以查找搜索词。您可能想了解如何创建前缀树,也称为 trie。这是比较字符串的一种更有效的方法:learnersbucket.com/tutorials/data-structures/…npmjs.com/package/trie-search
  • 谢谢你我会检查的
猜你喜欢
  • 2018-06-22
  • 2021-07-29
  • 2021-11-08
  • 2019-03-03
  • 2017-04-28
  • 2018-12-09
  • 2020-05-16
  • 2020-10-07
  • 2020-10-05
相关资源
最近更新 更多