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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:30:04+00:00 2026-06-01T13:30:04+00:00

I am trying to create a 2 dimensional array but am getting this error.

  • 0

I am trying to create a 2 dimensional array but am getting this error.

I loop an object and try to assign them but it won’t let me assign a value to the second dimension.

This is what I have:

//this is globally set
var gcollision = new Array();

function create() {
    for (var X in sdata) {
        X = parseInt(X);
        for (var Y in sdata[X]) {
            Y = parseInt(Y);
            width = parseInt(sdata[X][Y][2]);
            height = parseInt(sdata[X][Y][3]);
            for (i = X; i != X + width; i++) {
                //error occurs here "Uncaught TypeError: Cannot set property '3' of undefined"
                gcollision[i][Y] = 1
                for (j = Y; j != Y + height; j++) {
                    gcollision[X][j] = 1
                }
            }
        }
    }

How do I make it set the value of properly?

EDIT sdata looks like this:

var sdata = {"4":{"7":["1","7","3","3"]},"3":{"3":["2","8","1","1"]}};
  • 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-01T13:30:05+00:00Added an answer on June 1, 2026 at 1:30 pm
    //this is globally set
    var gcollision = new Array();
    
    function create(){
        for (var X in sdata) {
            X = parseInt(X);
            for (var Y in sdata[X]) {
                Y = parseInt(Y);
                width = parseInt(sdata[X][Y][2]);
                height = parseInt(sdata[X][Y][3]);
    
    for (i=X; i!= X+width; i++) {
                     if( typeof gcollision[i] == 'undefined' ) gcollision[i] = new Array();
                     gcollision[i][Y] = 1
    
    for (j=Y; j!=Y+height; j++) {
                     if( typeof gcollision[X] == 'undefined' ) gcollision[X] = new Array();
                     gcollision[X][j] = 1
                    }
                }
            }
        }
    

    Basically, even though you created your array, those indices do not exist yet, so you cannot reference them as arrays until after you define them as such.

    If you set up your for loops a bit more optimally, you don’t have to do isset and can just create the gcol[index] = Array(); the right before the inner loop where it is first accessed.

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

Sidebar

Related Questions

I am trying to create a multi dimensional array using this syntax: $x[1] =
I'm trying to create a two dimensional array like this... @transactions = {} @expenses.each
I'm trying to create a basic for loop which creates an array but am
I am trying to create a multi-dimensional array whose parts are determined by a
I'm trying to create a 2 dimensional array of Node objects as follows public
I am trying to use 2 dimensional array in javascript for storing strings. But
I am trying to create a global 2-dimensional array who's size would depend on
Im trying to create a 2 dimensional array as a cookie in javascript. Is
I am trying to create a multi dimensional array using $whole_array=array($case_id1,$case_name1,$case_status1); (these are variables
Excuse the newbie question. I'm trying to create a two dimensional array in ruby,

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.