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

The Archive Base Latest Questions

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

I have an object which im converting into an array of coords which will

  • 0

I have an object which im converting into an array of coords which will be used to store which coords are “occupied”.

I thought i had it working but on further inspection through console.log the array is missing a fair few results.

So this is basically my object:

var sdata = {"4":{"7":["1","7","3","3"]}};

In words this is:

var sdata = {"X":{"Y":["ID","ID","Width","Height"]}

Ignore ID for this as they are unrelated… but im trying to use this data so that i have the X and Y + the additional X and Y coords related to its tile dimensions width and height.

Imagine if you will the object for 4:7 is 3 by 3 dimensions so resulting in these 9 grid references would exist.

[4:7], [5:7], [6,7]  
[4:8], [5:8], [6,8]  
[4:9], [5:9], [6:9]

So my function to create the coords is :

function populate_collisions() {

  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
        }
      }
    }
  }
}

But my logic must be wrong because im getting this result for my array:

[4] [7] = 1
[4] [8] = 1
[4] [9] = 1
[5] [7] = 1
[6] [7] = 1

Any idea why im missing additional data?

  • 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-01T11:30:02+00:00Added an answer on June 1, 2026 at 11:30 am

    Here’s the fixed code:

    function populate_collisions() {
    
      for (var X in sdata) {
        X = parseInt(X);
    
        for (var Y in sdata[X]) {
          Y = parseInt(Y);
          var width = parseInt(sdata[X][Y][2]);
          var height = parseInt(sdata[X][Y][3]);
    
          for (var i=X; i < X+width; i++) {
    
            if (!gcollision[i]) {
              gcollision[i] = [];
            }
            gcollision[i][Y] = 1;
    
            for (var j=Y; j < Y+height; j++) {
    

    In the next part, you were using X, which is always 4 so you were just overwriting that array. Use i instead of X.

    You don’t need the if test, since the array is created above, unless you really meant gcollision[X][j] or similar. I don’t know as I don’t know the structure you want for the gcollision object.

    /*
              if (!gcollision[i][j]) { 
                gcollision[i][j] = [];
              }
    */          
              gcollision[i][j] = 1;
            }
          }
        }
      }
    }
    

    For the record, the resulting object (assuming gcollision has no other properties) is:

    alert(JSON.stringify(gcollision));
    /*
    {"4":[null,null,null,null,null,null,null,1,1,1],
     "5":[null,null,null,null,null,null,null,1,1,1],
     "6":[null,null,null,null,null,null,null,1,1,1]}
    */
    

    All those nulls don’t exist, the arrays are sparse, but that’s how JSON represents them.

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

Sidebar

Related Questions

I'm working with a CSV file in python, which will have ~100,000 rows when
I have a function which extracts a file into a byte array (data). int
I have a complex object(tree structure) which I am flattening it out into a
I have object A which in turn has a property of type Object B
I have object A which contains multiple instances of object B, which in turn
I have an object which straight forward instance variables. Some are NSString, some are
I have this object which is an instance of a superclass. I want to
I have an object which contains models for my ASP.NET MVC web app. The
I have an object which has several properties that are set when the object
I have an object which needs to destroy itself. Can it be done? Is

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.