After receiving a response from the API, the JSON data looks like this:
[
{
"id": 6,
"nombre": "Pechuga de pollo",
"categoria": "Pollo",
"existencia": 100
},
{
"id": 7,
"nombre": "Pierna de pavo",
"categoria": "Pollo",
"existencia": 100
},
{
"id": 8,
"nombre": "Lonja de pescado",
"categoria": "Pescado",
"existencia": 200
},
{
"id": 9,
"nombre": "Coca Cola",
"categoria": "Bebida",
"existencia": 200
},
{
"id": 10,
"nombre": "Jugo de naranja",
"categoria": "Bebida",
"existencia": 200
}
]
To filter this JSON based on the "categoria" value, I need to populate three different select inputs in my template.
I attempted using the filter() method but it seems my approach is not correct:
///This function specifies the filtering condition:
filtradoBebida(bebida){
bebida.categoria == "Bebida"
},
///Here's where I apply the filter method:
filtrarProductos(){
this.bebidas = productosLista.filter(filtradoBebida)
}
The goal is to fill one select with items where "categoria" is equal to "Bebida" and another select input with items where the category is "Pollo".
"bebidas" represents an array from my data while ""productosLista" stores the API response as an array.
If you have any alternate methods for populating a select element in Vue.js by filtering values from a JSON data, please share your insights.