Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda