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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:55:31+00:00 2026-06-17T22:55:31+00:00

I want to fill a 2d array, and I have this code in javascript:

  • 0

I want to fill a 2d array, and I have this code in javascript:

n=2; //rows
m=3; //columns
z=0;
array1=[];
for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        array1[i]=[];
        array1[i][j] = z;
        console.log(i+","+j+"="+z); //debug
        z++;
    }

}

console.log(array1);

But instead of getting this expected result;

[[0, 1, 2], [3, 4, 5]]

I get:

[[undefined, undefined, 2], [undefined, undefined, 5]]

why!? I don’t understand why I am getting undefined for all the items in each “inner” array, except the last one which is correct (here, the 2 and 5).

I made a debug step logging each i,j pair and the assigned z value, and i DO get the correct pair-values each time (i,j=z):

0,0=0
0,1=1
0,2=2
1,0=3
1,1=4
1,2=5

so, I think I am correctly filling the array using arr[i][j]=z each time, so why does it get undefineds for those cases?
I also tried using the arr=new Array() instead of arr=[] syntax in both cases, but with the same result.

Any ideas? I’m almost sure it will be a trivial error, but I can’t find what is wrong!.

Thanks.

  • 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-17T22:55:32+00:00Added an answer on June 17, 2026 at 10:55 pm

    You are reinitializing the empty array in every loop iteration:

    array1[i]=[];
    

    When you should only be doing this once per value of i. Move the initialization out of the inner-most loop.

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

Sidebar

Related Questions

I want to fill an array in javascript that takes all the values out
Actually I want to populate an array to fill up my table view, but
I am trying to fill a table view with an array, but I want
I have a Spinner and I want to fill this one with a currency
I want to fill a ListView without an array of all the same things.
I want to fill all cells with the same control. What I have now
I have an ImageButton that I want to fill its parent RelativeLayout container for
I have an Android application with a Spinner and want to fill it dynamically
here's my question, i want to fill the content div with white. BUT when
I have this code which calculates the sum of "letter numbers" in a string,

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.