0

This should be a very simple task, but for some reason I´m not seeing my mistake. I´m sorry if it is too obvious.

I have created an Array with multiple items inside it (it is the first log in the picture). Some of this items are Arrays. I want to retrieve the first item from the first array. It should be quite simple. The name of my Array is boardAsArray, so I should just type:

boardAsArray[1][0];

And since the second item of this array is an Array, I should get the first item of the array inside of the array. But this is not what´s happening.

Here is the picture of the logs:

enter image description here

Here is what I´ve asked the code to log (in this order):

Logger.log(boardAsArray);
Logger.log(boardAsArray[1]);
Logger.log(boardAsArray[1][0]);
Logger.log(boardAsArray[1][1]);
Logger.log(boardAsArray[1][2]);
Logger.log(boardAsArray[1][3]);
Logger.log(boardAsArray[1][4]);
Logger.log(boardAsArray[1][5]);
Logger.log(boardAsArray[1][6]);

As you can see in the picture. Instead of getting the first item of the first array, the code is logging the first array apparently as a string.

How do I fix this? What am I doing wrong?

I´m really sorry if this question is not very clear, but here is the thing. I´m don´t really understand what´s going on. It is so messy that I can´t even explain it...

Given this array (the first log in the picture):

Logger.log(boardAsArray[1][0])
// Should be 6 and not [

Sorry again for the confusion. And thank you very much for your help.

1 Answer 1

1

Within the element boardAsArray[1] everything is a string. Hence boardAsArray[1][0] gives the value [ and so on. Make sure that it is an array.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a easy way of doing this? In order to create boardAsArray, I used: boardAsArray.push(somearrayhere); Could that be the problem?
check the type of somearrayhere? I need to see some code to be able to help. It seems somearrayhere is a string. Try logging the typeof somearrayhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.