Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7052809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:25:42+00:00 2026-05-28T03:25:42+00:00

I am trying to generate a 2D n*m array in javascript full of zeros.

  • 0

I am trying to generate a 2D n*m array in javascript full of zeros. What is the fastest way of doing that?

I know the simple for loop would be enough to set all the elements to 0, but what I would like to know, why can’t I do that with mapping. For example with the underscore lib (or even the native map)

_.map(Array(n),function(a){return 0}) // makes {undefined,undefined,...}

while

_.map([1,2,3,5,6],function(a){return 0}) // makes {0,0,0,0,0}

Can anyone explain if I can fill an empty array with a map function and how, or why not?

PS: There is a trivial solution to my problem, I am just asking this cause I would like to learn more, and I cant find a good enough answer on google. Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:25:42+00:00Added an answer on May 28, 2026 at 3:25 am

    Typical JavaScript .map() functions ignore array members that are undefined. That’s why Array(n) doesn’t work.


    You could easily add a method to Array.prototype to do a quick fill…

    Array.prototype.fill = function(n,v) {
        n = n || this.length;
        for( var i = 0; i < n; i++ ) 
            this[i] = v === void 0 ? i : v;
        return this;
    };
    

    then…

    Array().fill(3); // [0,1,2]
    
    Array().fill(3, 0); // [0,0,0]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to generate some Javascript for a Comet callback. The code that
I'm trying to parse the rows in a table that I generate using Javascript
I am trying to generate equivalent MD5 hashes in both JavaScript and .Net. Not
I am trying to build a 3D Javascript array, but I am unsure of
I have this snippet where I am trying to generate array of array of
i am trying to generate dynamic array as foreach($this->data['Carcase'] as $key=> $value) { if(!empty($value))
I am trying to generate an array with N integer values between 0 and
I'm trying to generate an array of 30 days from the current date and
I'm trying to find an elegant way in Coffeescript to merge an array of
This is a Javascript / jQuery question: I'm trying to generate six unique random

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.