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 () {
});
}
내가 원하는 것은 전체 json 객체 대신 범주 값 "categ1"이있는 데이터 만 가져오고 싶습니다. 어떻게해야합니까?
응답이 객체 배열 인 경우 Array.prototype.filter 를 사용할 수 있습니다 .
const result = response.filter(item => item.category_id === 'categ1');
결과 배열에는 categ1을 categoryId로 사용하는 개체가 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다