Is it possible to remove the contents of the array based on the index? If I have 2 arrays like these:
Array1 that contains 15 values and I want to get the last 10 values.
Before removing elements:
array1 == [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
After removing elements:
array1 == [5,6,7,8,9,10,11,12,13,14]
Array2 that contains 15 values and then I want to get only the first 10 values.
Before removing elements:
array2 == [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
After removing elements:
array2 == [0,1,2,3,4,5,6,7,8,9]
But there are conditions that must be fulfilled:
if the array only contains 3 elements is not necessary to discard the elements in the array, as well as if the array contains 10 elements only. but if the array contains more than 10 elements, the excess is discarded.
To keep the first ten items:
or, perhaps less intuitive:
To keep the last ten items:
You can use a negative value for the first parameter to specify length – n, and omitting the second parameter gets all items to the end, so the same can also be written as:
The
splicemethod is used to remove items and replace with other items, but if you specify no new items it can be used to only remove items. To keep the first ten items:To keep the last ten items: