【问题标题】:Vue JS filter by multiple array object itemsVue JS按多个数组对象项过滤
【发布时间】:2020-01-13 12:51:06
【问题描述】:

我有一些代码可以根据搜索输入过滤对象数组,它根据search 变量进行过滤。我希望能够基于此进行搜索,并可能在我的对象中搜索其他键:

import HelpGuides from '~/static/help/help-guide.json';

export default {
  head: {
    title: 'Help'
  },
  data () {
    return {
      guides: HelpGuides,
      search: ''
    }
  },
  computed: {

    filteredGuides: function() {
      return this.guides.filter(guide => {
        return guide.title.toLowerCase().includes(this.search.toLowerCase())
      })
    }

  }
}

上面是我的代码,它根据search输入过滤title键,然而,每个对象包含titletagsbodytags是一个数组,@987654329 @ 是一个字符串。

我该怎么做呢?

【问题讨论】:

    标签: javascript vue.js vuejs2


    【解决方案1】:

    使用|| 运算符匹配其他键

    filteredGuides: function() {
      return this.guides.filter(guide => {
        return guide.title.toLowerCase().includes(this.search.toLowerCase())
          || guide.body.toLowerCase().includes(this.search.toLowerCase())
      })
    }
    

    处理不同的类型取决于对象结构。另外,请记住,您可以在另一个函数中提取 || 子句。

    【讨论】:

      猜你喜欢
      • 2020-01-02
      • 2021-10-04
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      • 2022-08-10
      • 2019-07-18
      • 2020-09-29
      • 1970-01-01
      相关资源
      最近更新 更多