Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

311
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda