Caveat: If you .fill() an Array with an object, all elements refer to the same instance (i.e., the object isn’t cloned)
So if you do something like:
let a = Array(3).fill([]) // create an array named a and fill it with 3 empty array
a[2].push('poop') // push new value to the element array at index 2
expect [ [], [], [2]]
But because all element refer to the same instance, so the result is: [[2], [2], [2]]
More details here: https://2ality.com/2018/12/creating-arrays.html
Comments
Post a Comment