Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the amount of data returned from a JSON file using fetch?

I have this fetch statement that returns 19 building names, but I only want 10; the following is what I attempted, but I still get 19 building names.

fetchBuildings(energyProgramId) {
  fetch(`http://localhost:1001/api/energyprograms/${energyProgramId}/buildings/?results=10`)
    .then(res => res.json())
    .then(json => {
      this.setState({
        isLoaded: true,
        buildings: json,
      })
    });
}

Is there something extra I need to add?

like image 261
user1724708 Avatar asked Dec 24 '22 03:12

user1724708


1 Answers

Here is an example:

1.fetch('http://jsonplaceholder.typicode.com/posts/')

The above URL gives array of objects with 100 elements because it originally is an array of 100 elements.

2.fetch('http://jsonplaceholder.typicode.com/posts/?_limit=10') This URL gives array of objects with 10 elements.

Notice the difference?

I only did this : ?_limit=10 ----> Add any number in place of 10 and hopefully you will get desired results.

like image 182
Syed Sher Ali Hussain Avatar answered Apr 30 '23 10:04

Syed Sher Ali Hussain