0

I'm getting an odd issue with a nested array I have in a program I'm working on.

When I print out the nested array in console.log as a set of values it prints out correctly.

Ex.

console.log("Array: " + arr) //Array: 0,0,0,0,0,0,0,1

However when I print them out as the object I'm getting entirely different values right after it. After expanding it out on chrome's developer tools I get.

console.log(arr)//(2)[Array(2), Array(2)]
0: [2][1,4]
1: [2][0,1]

Sometimes javascript/developer tools are weird and so I'm consulting SO.

I don't believe I have pointer issues where between the two print statements something is accessing a value in the arr. They happen right after eachother and the basic pseudocode on the main nested array manipulation is a simple increment of +1.

I couldn't find anything online, but wanted to confirm there are no odd issues with chrome developer tools or javascript when printing objects vs. values. Unfortunately, I believe this is as concise an example I can make and don't know what example code would be otherwise helpful in this case.

One other odd issue I'm seeing on the same matrix object that when I try to access an element i.e arr[0][0][0] I'm getting an entirely different value than I would have expected.

Thanks in advance and if there turns out to be no known issues/considerations with either the developer tools or javascript then I will close this post.

Update: In the developer tools this line when expanded:

Array element from console.log

Returns an entirely new set of values. As you can see, the 14 became 67 when expanded out. enter image description here

andor kesselman
  • 1,089
  • 2
  • 15
  • 26
  • How did you create the array? Consoles are not standardised, they try to be helpful. – RobG Jan 15 '18 at 00:21
  • Basic array creation was: //create three arrays {array1 = new Array(size)} then place array 3 into each element of array 2 and array 2 into each element array 1. I did a deep clone of each slice of the array when I generated the nested matrix to ensure that memory allocation was unique per element. I would have put more details on it, but the array seems to resolve correctly on the first print statement and is what I would expect. The second print statement right after the first one doesn't line up with expectations, although it is the same object printed directly after eachother. – andor kesselman Jan 15 '18 at 00:26
  • You need to show more of the code that you are using. – some Jan 15 '18 at 00:43
  • 1
    "*I don't believe I have pointer issues where between the two print statements something is accessing a value in the arr.*" - the issue with the references is when you modify the logged objects *after* they were logged (and before you look at them in the console). – Bergi Jan 15 '18 at 00:47
  • The Is `console.log() async or sync?` in particular is helpful. Thank you. I'm guessing it has something to do with the lazy evaluation however I don't think I ever increment up to the values I'm seeing. For example, the 67 value seems hard to believe an issue with synchronization. I will look into my code to review. I will not close this thread yet until I have had the chance to confirm duplication. – andor kesselman Jan 15 '18 at 00:51
  • I played around with `firefox's` developer tools and the issue no longer seems to be present. blurg. Thanks for the help everyone. Wish I had considered it an browser issue sooner. Guess it's time to welcome firefox .... – andor kesselman Jan 15 '18 at 00:59

1 Answers1

0
"Array: " + arr

This creates a string, by calling arr.toString() (that's how + works). Array's toString() joins the toString()s of all of the elements by commas. Therefore, if your array is nested, it will recursively flatten it.

console.log(arr)

This passes the array itself to console.log(), which has special handling to display objects.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • That doesn't explain why new values are generated that aren't there when printing it out by string. i.e as an example [0,0,1] turns to [4,0,3] directly after eachother. – andor kesselman Jan 15 '18 at 00:32
  • I've added an update to the thread with an example of generated numbers that don't align between the initial array and the expanded array. – andor kesselman Jan 15 '18 at 00:41