【发布时间】:2019-10-26 05:59:46
【问题描述】:
我从 API 收到此响应:
[
{
"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
}
]
所以我需要通过值“categoria”过滤这些json,然后我将在我的模板上填充三个不同的选择输入。
我尝试了 filter() 方法,但我想我做错了:
///This is the function that filter needs on the argument:
filtradoBebida(bebida){
bebida.categoria == "Bebida"
},
///This is the function where I apply the filter method:
filtrarProductos(){
this.bebidas = productosLista.filter(filtradoBebida)
}
我想用值 categoria == "Bebida" 的 json 填充一个选择,用值 == "Pollo" 的 json 填充其他选择输入。
“bebidas”是我数据中的一个数组 ""productosLista" 是一个从 API 接收响应的数组。
你知道另一种通过过滤 json 值在 vuejs 中填充选择的方法吗?
【问题讨论】:
-
您缺少
filtradoBebida中的return 语句。 -
另外,调用该方法似乎应该是
this.filtradoBebida。 -
我们需要更多上下文,我不知道
filtradoBebida是什么,但它看起来像一个对象的属性,如果是这样,你将不得不使用productosLista.filter(this.filtradoBebida)
标签: javascript json vue.js