I am working with an array of objects
var array = [
{id: true, category: "cat1", name: "test1"},
{id: true, category: "cat2", name: "test2"},
{id: true, category: "cat3", name: "test3"},
{id: true, category: "cat4", name: "test4"},
{id: true, category: "cat5", name: "test5"}
]
I am trying to filter this array based on matching values. Here is the script I am using:
let filtered = this.array.filter((array) => {
return array.name.toLowerCase().includes(this.keyword.toLowerCase());
});
var category = [];
if(category.includes("All")) {
return filtered;
} else {
return filtered.filter((array) => {
var keys = Object.keys(array);
var matchFilter = false;
category.forEach((key) => {
if(array[key] === true) {
matchFilter = true;
}
});
return matchFilter;
});
}
Additionally, I have implemented checkboxes on the frontend to store values for the variable category
<input type="checkbox" v-model="selectedCategory" value="All">
<input type="checkbox" v-model="selectedCategory" value="1">
<input type="checkbox" v-model="selectedCategory" value="2">
<input type="checkbox" v-model="selectedCategory" value="3">
<input type="checkbox" v-model="selectedCategory" value="4">
<input type="checkbox" v-model="selectedCategory" value="5">
Currently, the filtering only considers the ID values. However, I aim to expand the filtering to include category and name values as well.