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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:22:22+00:00 2026-05-18T20:22:22+00:00

I created the following 2D array in Javascript // Create basic linear array var

  • 0

I created the following 2D array in Javascript

// Create basic linear array
var ImgArray = new Array(4);

// Do the 2D array for each or the linear array slots
for (i=0; i < 4 ; i++) {
    ImgArray[i] = new Array(4)
}

Now i want to iterate through it using 2 ‘ enhanced for loops’. But am stuck on how to use the loop as there is only ImgArray stated a such. For example;

// Load the images
for(var i in ImgArray) { 
    for( ??? ) {           // How would i do this? What do i state as an array?
          ///...
    }
    document.write("<br>");
}

Any advise well appreciated

  • 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-18T20:22:22+00:00Added an answer on May 18, 2026 at 8:22 pm

    Assuming the array you’ve created, the loop looks like this:

    var i, j, entry, ImgArray;
    
    // Create the array
    ImgArray = new Array(4);
    for (i=0; i < 4 ; i++) {
        ImgArray[i] = new Array(4);
    }
    
    // Loop through both dimensions
    for (i = 0; i < ImgArray.length; ++i) {
        entry = ImgArray[i];
        for (j = 0; j < entry.length; ++j) {
            // Do something with entry[j]
        }
    }
    

    This is because there are no two-dimensional arrays in JavaScript. (In fact, even arrays aren’t really arrays, but let’s not go there.) There are “arrays”, and an array entry can be another array, but one array entry might be longer or shorter than others. So you retrieve that array and loop through its length, which may be different than others in the same “dimension”.

    Note that I didn’t use for..in above. Don’t use for..in to loop through arrays unless you really know what you’re doing; details here. (If you do really know what you’re doing and take adequate precautions, it’s fine, but your quoted code isn’t taking the necessary precautions.) for..in does not iterate the indexes of an array, it enumerates the property names of an object.

    Off-topic #1: In JavaScript, the convention (which you’re free to ignore) is to only use initial caps (ImgArray) for constructor functions.

    Off-topic #2: You might look at using array literals ([entry, entry, entry]) rather than new Array(...), but it depends on what you’re doing.

    Off-topic #3: It’s a very bad idea to rely on semicolon insertion (as with your ImgArray[i] = new Array(4) line). Make sure to put in the semicolons where they’re needed, or you’ll find that you can’t minify your scripts properly and/or that you’ll fight odd bugs that waste your time. 🙂

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

Sidebar

Related Questions

I created the following 2D array in Javascript // Create basic linear array var
I am trying to iterate a multidimension array created with the following line To
I want to create an associative array: var aa = {} // Equivalent to
I have the following array setup, i,e: var myArray = new Array(); Using this
I created the following interface: <?php interface Action { public function execute(\requests\Request $request, array
I am trying to dynamically create a Bitmap Image using byte array using following
I use the following code try to create an array of string vectors, I
I created the following function to simplify a piece of particularly complex code. CREATE
I have created the following stored procedure.. CREATE PROCEDURE [dbo].[UDSPRBHPRIMBUSTYPESTARTUP] ( @CODE CHAR(5) ,
I'm fairly new to web design, I've just created following website http://www.janewaltonwatercolours.co.uk , and

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.