Javascript 获取具有特定属性axios的数组对象
我通过axios从API调用中获取JSON,目前使用vue显示JSON 以下是控制台中的JSON对象:Javascript 获取具有特定属性axios的数组对象,javascript,vue.js,axios,Javascript,Vue.js,Axios,我通过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: categ
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):
我想做的是只获取类别值为“categ1”的数据,而不是整个json对象。如何实现这一点?如果响应是一个对象数组,则可以使用
const result=response.filter(item=>item.category_id==='categ1');
在结果数组中,您将拥有categ1作为categoryId的对象,您可以在响应是对象数组的情况下使用
const result=response.filter(item=>item.category_id==='categ1');
在结果数组中,您将有categ1作为categoryId的对象jquery?为了什么?jquery?为了什么?你不需要解析它axios为你这么做为了捕获,更新答案以使用
filter
而不是find
并删除JSON。解析
你不需要解析它axios为你这么做为了捕获,更新了使用filter
而不是find
的答案,并删除了JSON.parse
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 () {
});
}