Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter json data while Fetching in Vuejs

Hi I want to filter datas when fetching. For example: in to do list I want to filter only finished datas. Here is part of my fetch code:

Json db: http://localhost:3000/yapilacaklar.

<script>
 mounted(){
    fetch('http://localhost:3000/yapilacaklar')
      .then ((res)=>res.json())
      .then ((data)=>this.yapilacaklar=data)
       // I want to add filter parameters here
      .catch((err)=>console.log(err))
  }
 
</script>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the computed for this:

computed: {
    // a computed getter
    yapilaFinished () {
      // `this` points to the component instance
      return this.yapilacaklar.filter((data)=> data.finished === true)
    }
}

or just when you get the data filter the data:

mounted(){
    fetch('http://localhost:3000/yapilacaklar')
      .then ((res)=>res.json())
      .then ((data)=>this.yapilacaklar=data.filter((d)=> d.finished === true))
       // I want to add filter parameters here
      .catch((err)=>console.log(err))
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading