You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you modify the length of the array, e.g. vm.items.length = newLength
To deal with caveat 2, you can also use splice:
example1.items.splice(newLength)
But when the newLength Is greater than the current length, this methods failure.
for example
I just update the docs to remove the also in you can also use splice. Hopefully, this makes it clearer that we recommend splice as an alternative to directly setting the length of an array.
As an aside, I don't actually recommend setting length to increase the size of an array in JavaScript. What you get at the end is not actually an array with 8 indices pointing to the value undefined, as it would appear, but rather 8 indices with undefined pointers. The difference is subtle, but has important implications. For example, if you try to map over this array with a.map(() => 3), what you'll get is [3, 3, undefined x 8], rather than [3, 3, 3, 3, 3, 3, 3, 3, 3, 3] as you might expect.
Activity
chrisvfritz commentedon Jun 21, 2017
I just update the docs to remove the
also
inyou can also use splice
. Hopefully, this makes it clearer that we recommendsplice
as an alternative to directly setting the length of an array.As an aside, I don't actually recommend setting
length
to increase the size of an array in JavaScript. What you get at the end is not actually an array with 8 indices pointing to the valueundefined
, as it would appear, but rather 8 indices with undefined pointers. The difference is subtle, but has important implications. For example, if you try tomap
over this array witha.map(() => 3)
, what you'll get is[3, 3, undefined x 8]
, rather than[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
as you might expect.