2

So, I have the following array inside of an array:

Log request:

console.log(cleanArray);

Output:

[Array[2], Array[5], Array[2], Array[2], Array[5], Array[8], Array[7], Array[2], Array[3], Array[2], Array[2], Array[2], Array[2], Array[2], Array[3], Array[3], Array[2], Array[4], Array[5], Array[2], Array[6], Array[6], Array[20], Array[2], Array[13], Array[71], Array[1], Array[3], Array[3], Array[2], Array[3], Array[2], Array[2], Array[47], Array[2], Array[132], Array[1], Array[56], Array[2], Array[5], Array[7], Array[9], Array[13], Array[11], Array[1], Array[14], Array[7], Array[7], Array[13], Array[12], Array[4], Array[11], Array[11], Array[25], Array[15], Array[18], Array[10], Array[5], Array[53], Array[30], Array[2], Array[6], Array[2], Array[1], Array[2], Array[2], Array[2], Array[3], Array[2], Array[1], Array[1], Array[2], Array[2], Array[2], Array[2], Array[1], Array[3], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[3], Array[1], Array[3], Array[3], Array[2], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[2], Array[11], Array[40]]

Properly, if you are to access cleanArray[0] you get the following output:

Log request:

console.log(cleanArray[0]);

You get the inner array:

["Christian", "Pulisic"]

However! When I do this in a loop:

for (i in cleanArray){
    console.log("This is the sentence we are looking at " + cleanArray[i]);
    console.log(cleanArray[0]);

The array disappears and I am getting:

This is the sentence we are looking at Christian,Pulisic

And, the log immediately below it, properly yields the inner array:

["Christian", "Pulisic"]

However, when I do the same exact loop elsewhere in my code I do not run into that problem. What is going on?!

0

1 Answer 1

3

You are doing a concat between a string and an object. Javascript knows no better than to call toString on that object (or simply cast that object to string). And the result of calling toString on that array is the values of the array separated by commas.

You can check this link out for more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString

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

Comments

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.