[Vuejs]-Nuxt JS / Vue JS search filter not working

0👍

In the example you give, you’re not passing an array of elements to the query. When each query param only has one value it is returned as a string. When it has more than one value e.g. test=1&test=2, it will return an array of strings.

I think what you are really looking to do is test if any query param matches as key. One way to do this is:

return Object.keys(self.$route.query).some(function (item) {
   return property[val].match(item).length > 0
 })

0👍

I think this wrong due to you didn’t use the ES6, self not recognized in it, you can use arrow function instead,

computed: {
 filteredProperties() {
    let routeConstraints = ['search', 'type', 'bedrooms', 'county'].filter((val) => {
        return this.$route.query[val] !== undefined
    })
    if (routeConstraints.length === 0) {
        return this.properties
    } else {
        return routeConstraints.reduce((acc, val) => {
            return acc.filter((property) => {

                return this.$route.query[val].some((item)=> {
                    //changed the matching condition to indexOf
                    return property[val].match(item).length > 0
                })
            })
        }, this.properties)
    }
}

}

Leave a comment