0
fetchData() {
      axios.get("api_url").then((response) => {
        this.dataArr = response.data;
      });
    },

I can't do this.dataArr = response.data.items.fruits; because I need to use this somewhere else like this.

dataArr looks like this:

{
   "items":{
      "fruits":[
         {
            "name":"banana",
            "type":"fruit",
    
         },
         {
            "name":"kiwi",
            "type":"fruit",
    
         },
  ]
}

I am trying to filter this in computed property:

filterData(){
   return this.dataArr.filter((item) => item.name )
}

But I am getting an error saying that this.dataArr is not a function, I need to get access to this.dataArr.items.fruits, but when I write it like that it's not working. Is there way to solve this?

1
  • Are you sure you're getting "this.dataArr is not a function"? Is doesn't make sense that using .filter() on this.dataArr would throw that error- filter() is an array prototype function. Commented Dec 16, 2020 at 19:01

1 Answer 1

1

You should get access to the nested fruits array and it seems that you're trying to map that array by returning only the names in this case use map method:

 filterData(){
     if(this.dataArr.items && this.dataArr.items.fruits){
      return this.dataArr.items.fruits.map((item) => item.name )
      }else{
        return []
     }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't work like that. Even when I try to compute return this.dataArr.items and then do a v-for in html with this computed it wont display anything
Yes but I think the issue is because I used nuxt.js

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.