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 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 have the following array setup, i,e: var myArray = new Array(); Using this
I have created the following stored procedure.. CREATE PROCEDURE [dbo].[UDSPRBHPRIMBUSTYPESTARTUP] ( @CODE CHAR(5) ,
Given an Oracle table created using the following: CREATE TABLE Log(WhenAdded TIMESTAMP(6) WITH TIME
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
I can create the following and reference it using area[0].states[0] area[0].cities[0] var area =
I have a situation, where I need to create a new JavaScript object that
I am using the following array to add an object that was previously created
I have the following array, it is currently created sorted by entity_count (outputted by
I am desperately trying to manually create a JSON-style array in Javascript to send

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.