How do I efficiently filter an array of JSON data?
Here is the code snippet:
<div v-if="vacciCounter">
<b-card no-body class="mb-2" v-for="(stateObject, state) in filteredList" v-bind:key="state">
<div style="margin:10px 10px 10px 10px;">
<h5>{{ state }}</h5>
Here is the filtering function:
computed: {
filteredList() {
if (this.vacciResults.length > 0) {
return this.vacciResults.filter(entry => {
return entry.state.toLowerCase().includes(this.searchstring.toLowerCase())
});
}
},
getStates() {
return this.vacciResults; // assuming that you have stored the response in a variable called 'output' defined in the data section
}
This is the API function fetching the JSON data: vacciResults contains the states information.
fetch(vacciURL, {
method: 'get',
headers: {
}
}).then(res => res.json())
.then(res => {
this.status = '';
this.searchBtnDisabled = false;
this.vacciCounter = res.vaccinated;
this.vacciResults = res.states;