I have created a simple Vue app that I would like to filter by both the name of a city as well as the code (value). Is this possible?

I know the right way to go is using Vue computed but for some reason I just can't link this one up.

https://jsfiddle.net/ax8fer9L/

new Vue({
  el: '#app',
  data() {
    return {
      findName: '',
      options: [
        {"text":"Aberdeen","value":"ABZ"},
        {"text":"Belfast","value":"BHD"},
        {"text":"Bradford","value":"BRF"}
      ]
    }
  },
  computed: {
    filteredNames() {
      let filter = new RegExp(this.findName, 'i')
      return this.options.filter(el => el.match(filter))
    }
  }
})

1 Answers

1
Hazza On Best Solutions

You just need to filter on the properties of your list of options. Check this fiddle: https://jsfiddle.net/zxua4r0m/

The changed code:

return this.options.filter(el => el.text.match(filter) || el.value.match(filter))