javascriptfetch

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?


Solution

  • No different from fetch to XHR or axios or anything else. actually, no different from react or angular or vue or anything else.

    This is an API that backend developers wrote it and it is based on REST API, so when you call it as GET or POST and anything else you just fetch the JSON that the backend developers designed it. BUT

    There is a new technology that name is GraphQL. you can call API and then you just fetch the JSON what you want. Also, it must be implemented in backend but it is possible.