0

I have some code which filters an array of objects based on a search input, it filters based on the search variable. I'd like to be able to search based on this, and potentially other keys in my object:

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())
      })
    }

  }
}

Above is my code, which filters the title key based on the search input, however, each object contains title, tags and body, tags is an array, and body is a string.

How would I go about doing this?

0

1 Answer 1

2

Use || operator to match other keys

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

Dealing with different types depends on object strcture. Also, remeber you can extract || clauses in another function.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.