I do a filter by clicking on the button should find a item category that matches the category of the button, leave these elements and hide the rest. Faced the problem that in the setSocialSortBy method I made an array that contains Index elements that have this category matchArr = [1,3,5 ...], but I donβt get now to filter this.items by this new array matchArr. How can I do that?
json ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΡΠΈΡ
ΠΎΠ΄ΠΈΡ Ρ api: {name: 'title 1', category: 'cat 1'}, {name: 'title 2', category: 'cat 2'}, {name: 'title 3', category: 'cat 1'}, {name: 'title 4', category: 'cat 2'} ... <button v-for="cat in cats" @click="setCatSortBy(cat)">{{cat}}</button> <div v-if="items.length > 0"> <div v-for="item in items">{{item.name}}</div> </div> <script> export default { data() { items: [], cats: [] }, created () { this.fetchItems(); }, methods: { fetchItems() { this.loading = true; fetch("/api/items") .then(res => res.json()) .then(res => { this.items = res; let catArr = []; this.items.forEach((item) => { catArr.push(item.category); }); catArr.filter((value, index, arr) => { if (arr.indexOf(value) === index) { this.cats.push(value.toLowerCase()); } }); }); }, setCatSortBy(name) { let matchArr = []; this.items.filter((value, index) => { if (value.category.toLowerCase() === name.toLowerCase()) { matchArr.push(index); } }); } } } </script>
jsonat least oneitemfrom/api/items- Rustam Gimranov