Muhammad Saad
Muhammad Saad

Reputation: 1

How to get only a finite number of entries using Fetch API?

I am using slice method in the code example below after getting a whole bunch of data from an API. Is there any method to query just a finite number of entries from API without loading bundle of JSON Data?

getDataBtn = document.getElementById("getData");
box  = document.getElementById("container")

getDataBtn.addEventListener("click", getData);

function getData(){
    url = "https://api.github.com/users";
    fetch(url).then((response)=>{
        return response.json();
    }).then((data)=>{
        
        //getting first five items only
        var myArray = data.slice(0,5).map((item)=>{
            x = { ID : item.id , Username : item.login , Github_URL : item.html_url } ;
            return res;
        })
        box.innerHTML = JSON.stringify(myArray);
    })
}

Upvotes: 0

Views: 560

Answers (1)

A J
A J

Reputation: 176

Here, you can read about additional query parameters for the above mentioned API - link

function getData(){
    url = "https://api.github.com/users?per_page=5";
    fetch(url).then((response)=>{
        return response.json();
    }).then((data)=>{
        box.innerHTML = JSON.stringify(data);
    })
}

Upvotes: 1

Related Questions