Truncate an Array
Every time I go to truncate and array in JavaScript, I have to look up the syntax. It's something that makes me angry.
Did you know that there is an oddly easier way to do it without reaching for splice()
or slice()
?
const collection = [4,6,9,1,12,42];
collection.length = 3;
collection
is now [4,6,9]
Hopefully I can remember this one!
Tweet