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

  • Home
  • SEARCH
  • 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 6077329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:42:31+00:00 2026-05-23T10:42:31+00:00

is there a shorter, better way to generate ‘n’ length 2D array? var a

  • 0

is there a shorter, better way to generate ‘n’ length 2D array?

var a = (function(){ var i=9, arr=[]; while(i--) arr.push([]); return arr })();

a // [ [],[],[],[],[],[],[],[],[] ]

** old-school short-way**:

var a = (function(a){ while(a.push([]) < 9); return a})([]);

UPDATE – Using ES2015

Array(5).fill().map(a=>[]); // will create 5 Arrays in an Array
// or
Array.from({length:5}, a=>[])

Emptying 2D array (saves memory rather)

function make2dArray(len){
    var a = [];
    while(a.push([]) < len); 
    return a;
}

function empty2dArray(arr){
    for( var i = arr.length; i--; )
      arr[i].length = 0;
}

// lets make a 2D array of 3 items
var a = make2dArray(3);

// lets populate it a bit
a[2].push('demo');
console.log(a); // [[],[],["demo"]]

// clear the array
empty2dArray(a);
console.log(a); // [[],[],[]]
  • 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-23T10:42:31+00:00Added an answer on May 23, 2026 at 10:42 am

    Another way:

    for(var a = [];a.length < 10; a.push([])); // semicolon is mandatory here
    

    Yet another way:

    var a = []; while(a.push([]) < 10);
    

    This works because .push() [docs] (specification) returns the new length of the array.


    That said, this is the wrong way of “reducing code”. Create a dedicated function with a meaningful name and use this one. Your code will be much more understandable:

    function get2DArray(size) {
        size = size > 0 ? size : 0;
        var arr = [];
    
        while(size--) {
            arr.push([]);
        }
    
        return arr;
    }
    
    var a = get2DArray(9);
    

    Code is read much more often than written.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better shorter way than iterating over the array? int[] arr =
Is there a better/shorter way to write the whoAmI method in the following code?
Is there a better (shorter?) way than the following? let cpucount = System.UInt16.Parse( reader.GetInt32(3).ToString()
Say you have an array, data, of unknown length. Is there a shorter method
Is there a better / cleaner / shorter way of getting the same output
There must be a better / shorter way to do this: # Find files
Is there a better way to do this function? Even though i'm doing the
Is there a better/shorter way to handle (lots of) null references, for example when
Is there a better shorter way to rewrite the code. I have 10 of
I was wondering if there is a shorter, better or cleaner way to assign

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.