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

230
Views
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 answers
Answer question

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 Report

0

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