JavaScript, like any other programming language, has many handy tricks that let us write our programs more easily. In this article, we will look at how to do different things that involve arrays, like truncating arrays, converting arrays to objects and filling arrays with data.
Truncating Arrays
In JavaScript, array objects have a length property that specifies the length of an array. It’s both a getter and setter property so in addition to being able to get the length of an array, we can also use it to set the length of an array.
Setting the 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.
Filling Arrays with Data
With ES6 or later, we can fill arrays with an array with new data with the fill
method. The fill
method takes up to 3 arguments. The first is the value
we want to add to the array.
The second optional argument is the start index of which we want to start filling data.
The default value of the second argument is 0. The third argument is an optional one that lets us specify the end index of to fill the array entry up to.
The default value for the third argument is the length
of the array. Note that the end index itself is excluded from the filling so it won’t overflow when we call the fill
method.
The fill
the method returns a new array with the new values filled in. All existing values that were filled will replace any existing values that were in the original array.
For example, we can use it in the following way:
let arr = [1, 2, 3, 4, 5, 6];
arr = arr.fill(8)
console.log(arr);
Then we get the following output from the console.log
:
[8, 8, 8, 8, 8, 8]
We can also change the index of the fill
method to specify the fill boundary. For example, we can write:
let arr = [1, 2, 3, 4, 5, 6];
arr = arr.fill(8, 3, 5)
console.log(arr);
Then we get:
[1, 2, 3, 8, 8, 6]
from the console.log
. If we specify an end index that’s over the length
of the original array, then it’ll replace the values up to the original array up to the last position of the original array. For example, if we have:
let arr = [1, 2, 3, 4, 5, 6];
arr = arr.fill(8, 3, 10)
console.log(arr);
Then we get:
[1, 2, 3, 8, 8, 8]
We can also use negative indexes to specify the fill boundary with the fill
method. The last position of the array would be -1, the second last would be -2, and so on. So we can call fill
with negative indexes like the following:
let arr = [1, 2, 3, 4, 5, 6];
arr = arr.fill(8, -4, -1)
console.log(arr);
Then we get:
[1, 2, 8, 8, 8, 6]
since the third argument doesn’t include the last index. If we want to fill the last element as well with the new value, then we just omit the last argument like in the following code:
let arr = [1, 2, 3, 4, 5, 6];
arr = arr.fill(8, -4)
console.log(arr);
We get [1, 2, 8, 8, 8, 8]
when we run the code above. If the second argument’s value is bigger than the third argument, then no fill operation will take place and the original array is returned. For example, if we have:
let arr = [1, 2, 3, 4, 5, 6];
arr = arr.fill(8, 4, 1)
console.log(arr);
Then we get back [1, 2, 3, 4, 5, 6]
which is the original array.
Conclusion
In JavaScript, array objects have a length property that specifies the length of an array. It’s both a getter and setter property so in addition to being able to get the length of an array, we can also use it to set the length of an array. This means that setting the length
property to be less than the original length of the array. We can also use the splice
and slice
methods to truncate arrays.
With ES6 or later, we can fill arrays with an array with new data with the fill
method. The fill
method takes up to 3 arguments. The first is the value
we want to add to the array. The second optional argument is the start index of which we want to start filling data.
The default value of the second argument is 0. The third argument is an optional one that lets us specify the end index to fill the array entry up to. The default value for the third argument is the length
of the array.
We can also use a negative index for the second and third arguments for setting the boundaries.