0

What’s wrong with my search function? It works for the first line (city values), but not for the others. If I remove everything but the cityValues line, or change the && to ||, the function works, but not like this.

I’m trying to search through an array of parameters on each value. Meaning: the user has the possibility to select multiple options from multiple dropdowns, and see the results.

For some context, cityValue, zipValue etc are arrays. cityValue is an array of strings, and the rest are arrays of numbers.

Thanks

   computed: {
            filteredPropertyTypes() {
                return this.propertyTypes.filter(rental => {
                    return this.cityValue.indexOf(rental.city) !== -1 &&
                        this.zipValue.toString().indexOf(rental.zip.toString()) !== -1  &&
                        this.bedroomsValue.toString().indexOf(rental.bedrooms.toString()) !== -1 &&
                        this.bathroomsValue.toString().indexOf(rental.bathrooms.toString()) !== -1 &&
                        rental.rent.toString().includes(this.rentValue.toString());
                })
            }
        },

1 Answer 1

1

According to MDN, The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

arr.indexOf(searchElement[, fromIndex])

The indexOf() method works on Arrays and looking at your function, you are using strings as your array.

For more on the indexOf() method, check here

EDIT -- based on the additional information, if cityValue, zipValue etc are arrays originally, using the toString() method on them causes them to become strings

enter image description here

Try changing your code to

computed: {
  filteredPropertyTypes() {
    return this.propertyTypes.filter(rental => {
      return this.cityValue.indexOf(rental.city.toString()) !== -1 &&
        this.zipValue.indexOf(rental.zip) !== -1  &&
        this.bedroomsValue.indexOf(rental.bedrooms) !== -1 && this.bathroomsValue.indexOf(rental.bathrooms) !== -1 && rental.rent.includes(this.rentValue);
    })
 }
},

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.