1

I am getting a JSON from an API call via axios and currently displaying the JSON using vue.

Here's my JSON Object from the console:

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

Here's my vue mounted function (i am using 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 () { 

    }); 
}

What I want to do is I want to only get the data that has a category value "categ1" instead of the whole json object. How do i achieve this?

1
  • jquery? for what? Commented Jul 5, 2020 at 18:30

1 Answer 1

3

You can use Array.prototype.filter in case the response is an array of objects.

const result = response.filter(item => item.category_id === 'categ1');

In the result array you will have objects with categ1 as categoryId

Sign up to request clarification or add additional context in comments.

2 Comments

you dont need to parse it axios doing it for you
Thanks for the catch, updated answer to use filter instead of find and removed JSON.parse

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.