0

So, I'm working on a JavaScript project. I building a self driving car in JavaScript.

I've built a random roads map generator class from which I need to return an Array of Arrays of Arrays of Arrays.

Array [Contains all Grid cells of Map] of Arrays [each is a cell containing borders Top, Bottom, Left, Right Border] of Arrays (each is a border containing xy to xy point of border line) of Arrays [each contains x, y point].

Why am I doing this? It is because each border line will be passed to each sensor of a car of detecting borders by the car.

[Image of Car sensor & road border Lines] (https://i.stack.imgur.com/b6EdD.png).

So the problem is that I return whole Array from a road map generation class function as

  availableBorders()
  {
    console.log(typeof(this.allborders));  // object

  return this.allborders;
  }

I receive this Array in Main Method as

const newRoadMap = new Maze(RoadMapSize, rowsColumns, rowsColumns);  //Instantiate Map Object
newRoadMap.setup();  // Set the Grid for Map Generation

let xx = (newRoadMap.availableBorders());

When i print xx on console it gives

console.log(" all border ",xx);

Image of Result of Console.log mentioned above

Okay!

But when I need specific value it says undefined.

console.log(xx[0]);    //undefined

console.log(xx[0][0]);   //undefined

console.log(xx[0][0][0]);    //undefined

console.log(xx[0][0][0][0]);     //undefined

console.log(typeof(xx));   //object 

I've been reading post upon post but can't find a solution to this problem. How can i access data in this array of arrays of arrays of arrays?

I've read so many post.

In my use case i am not allowed to use any other library.

Someone mention call .toObject onto the array & then use it.

.toObject does'not exist.

  • if `xx[0]` is undefined, then `console.log(xx[0][0])` would throw an error ... so, not sure how your possibly not throwing an error when trying to `console.log(xx[0][0])`, `console.log(xx[0][0][0])` and `console.log(xx[0][0][0][0])` – Jaromanda X Nov 25 '22 at 00:54
  • try `console.log(JSON.stringify(xx, null, 2))` - what's the output now – Jaromanda X Nov 25 '22 at 00:55
  • 1
    Sounds like the array is being populated asynchronously – CertainPerformance Nov 25 '22 at 01:00
  • @CertainPerformance Yes, I just found out that the array is being populated asynchronously. I called the array return function later in my main function and it has data now. How can i make it synchronous? so that previous functions are synchronous for a while until data is filled. – Zain Khalid Nov 26 '22 at 05:48

0 Answers0