I am using a tabbed interface built with Bootstrap-Vue. When clicking on one of these tabs I'm using v-for to render some data in an accordion/dropdown which is displayed as a grid.
Currently, I have a tab that shows all results which works fine when using the following (Note: prizesData is just where I'm storing response.data after I have called in with axios):
<div class="col-sm-12 col-md-6 col-lg-3 w-100 prize-dropdown-wrapper pb-5"
v-for="(prize, name, index) in prizesData"
:key="index"
>
<!--REMAINDER OF MARKUP GOES HERE-->
</div>
My JSON structure looks something like this, except I have 43 results in total. Every value in each object will change, but I want to use prizeType1Name and prizeType2Name to check if they contain the strings "CTW", "IW" or "OG":
{
"1": [
{
"prizeTitle": "",
"prizeImage": "",
"prizeFirstLineText": "",
"prizeType1": {
"Image": "",
"Line1": "",
"Line2": ""
},
"prizeType2": {
"Image": "",
"Line1": "",
"Line2": ""
},
"prizeTermsText": "",
"prizeType1Name": "CTW",
"prizeType2Name": ""
}
],
"2": [
{
"prizeTitle": "",
"prizeImage": "",
"prizeFirstLineText": "",
"prizeType1": {
"Image": "",
"Line1": "",
"Line2": ""
},
"prizeType2": {
"Image": "",
"Line1": "",
"Line2": ""
},
"prizeTermsText": "",
"prizeType1Name": "IW",
"prizeType2Name": ""
}
]
}
To access either of these values I am using:
this.prizesData[name][0].prizeType1
or this.prizesData[name][0].prizeType2
I'm having difficulty understanding how to filter my results. For example, if I click on the tab named (CTW), I want to show only the results that contain "CTW" in the JSON file. Or if the tab named "IW" is clicked then the results should be for the objects that contain "IW".
What I have tried:
Added filteredPrizes to store filtered prizes as an object to access later
data() {
return {
prizesData: {},
filteredPrizes: {},
};
}
I tried to use filter() on the existing prizesData object I received from the JSON file, I added this in the same method where I'm fetching the JSON file.
getPrizesInfo() {
const self = this;
//Get Error Validation JSON
axios
.get("/data/prizes.json")
.then(response => (this.prizesData = response.data));
this.filteredPrizes = this.prizesData.filter(prize =>
this.prizesData[name][0].prizeType1.includes("CTW")
);
}
What I'm expecting: I have 4 tabs, 1st one works fine (Shows all). The other 3 need to show only filtered results depending on if "CTW" "IW" or "OG" exist in either prizeType1Name or prizeType2Name.