length property to be less than the original length of the array will truncate the array up to the number of entries that we specify. For example, if we want to only keep the first entry of an array, then we can write something like:
let arr = [1, 2, 3, 4, 5, 6]; arr.length = 1 console.log(arr);
We simply set the
length property of our array and then magically we get only the first element of the array left.
Alternatively, we can use the
splice method to remove entries from an array. This method lets us removing, replace an existing element or adding new elements in place, but we only need it to delete elements from the array.
The argument of the
splice method is the starting index
start of which to start changing the array. It can be positive or negative. If it’s negative, then it’ll start changing the array from the end and move towards the start of the array. The end index is -1, the second is -2 and so on.
The second argument of the
splice method is the
deleteCount , which is an optional argument that lets us specify how many items to delete starting from the
start parameter in the first element.
Subsequent arguments are the items that we want to insert into an array. This can go on for as long as we want. Only the first argument is required.
For example, if we want to truncate an array and keep only the first 2 elements, we can write something like the following code:
const arr = [1, 2, 3, 4, 5, 6]; arr.splice(2) console.log(arr);
As we can see we only need to specify the first argument, and then the
deleteCount will be automatically set so that it removes the entries on the right of the array. We can also specify a negative
start index like in the following code:
const arr = [1, 2, 3, 4, 5, 6]; arr.splice(-1 * arr.length + 1) console.log(arr);
So if we specify
-1 * arr.length + n in the first argument, then we keep the first
n entries of the original array.
We can also use the
slice method to truncate arrays. It’s used for extracting values from an array in general and returns a new array with the extracted values. The
slice method takes 2 arguments.
The first is an optional argument to specify where to begin extracting entries from an array. The second argument is another optional argument that specifies the end index of the original array to end the extraction of values from it. The value at the end index itself is excluded from the extraction.
For example, if we want the first 2 values from the original array, then we can write something like the following code:
let arr = [1, 2, 3, 4, 5, 6]; arr = arr.slice(0, 2); console.log(arr);
When we run the code, we get back
[1, 2] as the new value of
arr . We can also specify negative indexes for the start and end. The last entry of the array is at -1, the second last element is at the index -2, and so on. So if we want to extract the first 2 elements of an array with negative indexes, we can write something like the following:
let arr = [1, 2, 3, 4, 5, 6]; arr = arr.slice(-1 * arr.length, -1 * arr.length + 2); console.log(arr);
The first element of an array would be at index
-1 * arr.length if we expressed it in terms of a negative index. Then the subsequent entries would be at
-1 * arr.length + n where
n is array entry at the
n th position.