Array truncation

If we need to truncate (modify an array by removing the elements), we have three main ways:

  1. .splice()

Just remember splice modifies, slice accesses. Negative numbers as first arg indicate index from the end of the array.

var firstArray = [2, 4, 6, 8];
var secondArray = firstArray.splice(-2, 2); // firstArray=[2,4], secondArray=[6,8]
  1. .slice()

If you're asking how to retrieve a subset of an array without modifying the original, then use .slice().

var firstArray = [2, 4, 6, 8];
var secondArray = firstArray.slice(-2); // firstArray=[2,4,6,8], secondArray=[6,8]
  1. Array.length

You can truncate the array, making it smaller by changing the array.length. See this example:

var numbers = [1, 2, 3, 4, 5];
numbers.length = 3; // now numbers is [1, 2, 3]

Note: if you assign a length which is longer than current length, undefined array elements are introduced, as shown below.

October 29, 2021 - Giorgio Grassini