chrisgrim
114
2
Vue

Filtering through array for specific name

Posted 1 month ago by chrisgrim

Hi, two questions here. First is if I am doing this a stupid over complicated way and second, why is the way I am trying not working?

I have a selector dropdown that allows a user to choose a country. In my vue this country is saved to data as an object. I then get the country.name from the object and save that to the database. This works great. The issue I am having is I want to repopulate the data if the user goes back to the form except now I no longer have an object, only the country name as a string.

My idea is that I should filter through the countries array searching for the name. Is that the smartest way of doing this? My second question (if this is the right way to do this) is what am I doing wrong with my code?

in my data for simplicity sake I have

keyword: 'Afghanistan',
countrydata: [],

when the page loads my country data looks like this

countrydata:Array[250]
0:Object
name:"Afghanistan"
1:Object
name:"Ă…land Islands"
2:Object
name:"Albania"

and so on

The country data is loaded using a mounted method that pulls it from restcountries.com. Finally I have my computed property which isn't working

computed: {
            countryFilteredList() {
                return this.countrydata.name.filter((country) => {
                    return this.keyword.toLowerCase().split(' ').every(v => country.title.toLowerCase().includes(v));
                });
            }
        },

Thanks!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.