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

318
Vistas
How to get a specific number of random entries from a JSON array?

I'm trying to show 10 random arcs using the globe.gl library. How can I load a random number of entries from a JSON array to achieve that? Currently, I have this code (full example on Codepen):

const capitalsUrl = "https://raw.githubusercontent.com/fallenartist/playground/main/json/capitals.json";
const world = Globe()(document.getElementById('globe'));
fetch(capitalsUrl).then(res => res.json()).then(capitals => {
    const dl = capitals.features.length;
    rand = Math.floor(Math.random() * dl);
    world
        .arcsData(capitals.features)
        .arcStartLat(() => capitals.features[rand].geometry.coordinates[1])
        .arcStartLng(() => capitals.features[rand].geometry.coordinates[0])
        .arcEndLat((d) => d.geometry.coordinates[1]) // I'd like 10 random entries here
        .arcEndLng((d) => d.geometry.coordinates[0]);// and here
});

How can I filter the loaded JSON data to limit it to random 10 entries, i.e. to show only 10 arcs?

Also, would it be possible to display another 10 random arcs after some time? The library proposes this is done with setTimeout() function but I was unsuccessful with that too:

setTimeout(() => {
    world
        .arcsData(capitals.features)
        .arcEndLat((d) => d.geometry.coordinates[1]) // I'd like 10 random entries here
        .arcEndLng((d) => d.geometry.coordinates[0]);// and here
}, 3000);

A D3 library could be used if it'd make this easier as I'm using this lib elsewhere. Thanks.

about 4 years ago · Juan Pablo Isaza
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