我正在通过axios从API调用中获取JSON,目前正在使用vue显示JSON。
这是控制台中的JSON对象:
0:
category_id: "categ1"
item_name: "item1"
price: 100
stock: 155
1:
category_id: "categ2"
item_name: "item2"
price: 100
stock: 155
2:
category_id: "categ1"
item_name: "item3"
price: 100
stock: 155
3:
category_id: "categ3"
item_name: "item4"
price: 100
stock: 155
这是我的Vue挂载函数(我正在使用axios):
mounted () {
axios.get('link_for_api_endpoint', {
headers : {
Authorization: 'Bearer ' + access_token,
},
params: {
limit: 250
}
})
.then((response) => {
this.data = response.data.items;
//console.log(response);
$("#ldr").hide();
removeLoader();
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});
}
我想做的是仅获取具有类别值“ categ1”的数据,而不是整个json对象。我该如何实现?
如果响应是对象数组,则可以使用Array.prototype.filter。
const result = response.filter(item => item.category_id === 'categ1');
在结果数组中,您将拥有categ1作为categoryId的对象
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句