Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
Javascript how to randomly select a specific amount of objects?

I have created a function that returns the login names of random users located on the following api website(https://api.github.com/users)

<script>
fetch('https://api.github.com/users').then(function(response) {
 
    response.json().then(function(users){
    document.write("Highlighred Github users")
    users.forEach(function(user){ 

for (let i=0; i< 8; i++) {
   document.write('<br>')
   document.write((users[Math.floor(Math.random() *users.length)].login))
}
    });
  });
}).catch(err => console.error(err));
</script>

Currently it does return random login names from that file, however, I wish to only randomly select 8 users from this list. My attempt at doing so can be seen in the for loop i < 8 however it prints alot more than 8(240 to be exact)

How can I have it so just returns 8 randomly? say for example

ezmobius
mojodna
kevwil
wayneeseguin
railsjitsu
bmizerany
atmos
fanvsfan
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

please, remove useless loop, i have commented out users.forEach(function(user){

fetch('https://api.github.com/users').then(function(response) {
 
    response.json().then(function(users){
    document.write("Highlighred Github users")
//     users.forEach(function(user){ 

for (let i=0; i< 8; i++) {
   document.write('<br>')
   document.write((users[Math.floor(Math.random() *users.length)].login))
}
    });
//   });
}).catch(err => console.error(err));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!