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 8976963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:16:00+00:00 2026-06-15T19:16:00+00:00

I want to create a 2-dimensional array with an index-number in each first element.

  • 0

I want to create a 2-dimensional array with an index-number in each first element.

(my previous question brought me to this point >)

this works:

$('#create_indexed_array').click(function() {
    var new_array = [[9,9],[9,9],[9,9],[9,9],[9,9]];
    for (var i = 0; i < 5; i++) {
        new_array[i][0] = i;
    }

    alert(JSON.stringify(new_array));

});

BUT this works not:

$('#create_indexed_array').click(function() {
    var new_array = new Array(new Array());
    for (var i = 0; i < 2; i++) {
        new_array[0][i] = ""; // create cols
    } 
    for (var i = 1; i < 5; i++) {
        new_array[i] = new_array[0]; // create rows
    }
    for (var i = 0; i < 5; i++) {
        new_array[i][0] = i; // set index
    }
    alert(JSON.stringify(new_array));

});

EDIT:
my final working version (so far):

var myArray  = [];
var rows = 5;
var cols = 2;

for (var i = 0; i < rows; i++) {
    myArray [i] = [];
    for (var j = 0; j < cols; j++) {
        if (j==0) myArray [i][j] = i;
        else myArray [i][j] = '';
    }
}

alert(JSON.stringify(myArray));

(r) mostly by jfriend 😉

still don’t know why it isn’t possible to declare the 2D array at the beginning with: myArray = [[]]

  • 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-06-15T19:16:01+00:00Added an answer on June 15, 2026 at 7:16 pm

    Following your current pattern, it will work like this:

    $('#create_indexed_array').click(function() {
        var myArray = [[]];
        for (var i = 0; i < 2; i++) {
            myArray[0][i] = ""; // create cols
        } 
        for (i = 1; i < 5; i++) {
            // create copy of first row in each other row
            myArray[i] = myArray[0].slice(0); 
        }
        for (i = 0; i < 5; i++) {
            myArray[i][0] = i; // set index
        }
        alert(JSON.stringify(myArray));
    });​
    

    Working demo: http://jsfiddle.net/jfriend00/vJDPp/

    One thing you have to remember is that assigning an array assigns a reference to that array, not a copy so if you want each element of the array to be different, you have to physically make a copy of the first row to put in the subsequent rows. I’d also recommend changing the name of new_array because that sounds so much like a function name that it makes the code confusing to read to someone who doens’t know it.


    If you’re just trying to initialize a 2D array to all 9’s like in your first code example, then this would be much simpler:

    $('#create_indexed_array').click(function() {
        var myArray = [];
        for (var i = 0; i < 5; i++) {
            myArray[i] = [];
            for (var j = 0; j < 2; j++) {
                myArray[i][j] = 9;
            }
        }
        alert(JSON.stringify(myArray));
    });
    

    Or a function version:

    function create2DArray(lenX, lenY, initVal) {
        var myArray = [];
        for (var i = 0; i < lenX; i++) {
            myArray[i] = [];
            for (var j = 0; j < lenY; j++) {
                myArray[i][j] = initVal;
            }
        }
        return(myArray);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a two dimensional array dynamically. I know the number of
I want to create a two-dimensional array in which I want to store records
There is this table i want to load into a multi-dimensional array. The problem
I have an array of two dimensional Arrays. I want to create a new
I want to create two dimensional array of buttons in windows form. I have
i want to create a single dimensional array in MATLAB inside for loop, please
I want to create a two dimensional array in a javascript function. I found
I want to create class representing n-dimensional array, but where is a commutative access
EveryBody.. i want to create one 8*8 dimensional array in objective c.. ( [0,
I want to create a 10 dimensional array that's filled with zeros. If I

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.