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

239
Vistas
How to create an array with a set amount of elements inside all set to 0? (Javascript)

If I want to create an array containing a piece of data for every minute of the year (525,600 elements for 365 day year) or (524,160 elements for 364 day year) is there a way to create an array with a set amount of elements inside all set to a value of 0?

const minutesOfTheYear = [];
minutesOfTheYear[0] = 0;
minutesOfTheYear[1] = 0;
minutesOfTheYear[2] = 0;
/* ... */
minutesOfTheYear[525,599] = 0;

I think I could use a for loop, setting the amount of loops to 525,600 and the index of the array amended to add by 1 each time, but what is the proper way to declare the new values within a for loop? Would this work?

for (let i = 0; i< 525599; i++) {
 minutesOfTheYear[i] = 0;
}

Or is there a line of code that just declares the size of the array and auto populates?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Use Array.from():

const result = Array.from({ length: 524160 }, () => 0);

console.log(result);

The second argument is a map function - it is run once for each item in the array, and it must return the value for that item. In this example, it's simply returning 0.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Array(100).fill(0)  // length is 100
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