Remove all items after an index

JavascriptJquery

Javascript Problem Overview


I have an array:

array = ['mario','luigi','kong']

I call its splice function to remove all items before an index:

array.splice(1) //-> ['luigi','kong']

I'm just wondering if there is a function similar to splice to remove all items after an index:

pseudo code

array.mirrorsplice(1) //-> ['mario','luigi']

Javascript Solutions


Solution 1 - Javascript

Use Array.length to set a new size for an array, which is faster than Array.splice to mutate:

var array = ['mario','luigi','kong', 1, 3, 6, 8];
array.length=2;
alert(array); // shows "mario,luigi";

Why is it faster? Because .splice has to create a new array containing all the removed items, whereas .length creates nothing and "returns" a number instead of a new array.

To address .splice usage, you can feed it a negative index, along with a huge number to chop off the end of an array:

var array = ['mario','luigi','kong'];
array.splice(-1, 9e9); 
alert(array); // shows "mario,luigi";

Solution 2 - Javascript

Though assigning a shorter value to the array length(as @dandavis said) is the fastest and simplest way to remove trailing element from an array, you can also do that using a similar method like splice which is known as slice. Like following:

array = ['mario', 'luigi', 'kong'];

array = array.slice(0, 2); //Need to assign it to the same or another variable

console.log(array); //["mario", "luigi"]

As you can see you need to store the returned value from slice method. To understand 'why', here are the major distinction between slice and splice method:

  • The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object.
  • The splice() method changes the original array and slice() method doesn’t change the original array.

Solution 3 - Javascript

To remove all items after an index:

var array = ['mario','luigi','kong'],
index = 1; // your index here
array.splice(index + 1, array.length - (index + 1) );
// 3 - (1+1) = 1
// 1 is the remaining number of element(s) in array
// hence, splice 1 after index

Result:

['mario', 'luigi']

You need to +1 since splice starts removing at the index.

Solution 4 - Javascript

I think you misunderstood the usage of Array.prototype.splice(). It already does what you asked for (remove everything after an index, read below paragraph for correction) and it does return the deleted values. I think you got confused with the returned value as the current value of the array.

Array.prototype.splice() however, removes the provided index value too, which is basically equivalent of setting the length of the array. So if you call it as array.splice(2), it'll set the length to 2 and everything including the values at index 2 and after will be deleted. This is provided that the current length of the array is greater than the first parameter provided to Array.prototype.splice().

For example:

const array = ['mario','luigi','kong'];
const deletedItem = array.splice(1);
console.log(array); // ['mario']
console.log(deletedItem); // ['luigi','kong']

For more information: refer to the MDN doc.

Solution 5 - Javascript

You can use splice. Here is a demo.

var array = ['mario','luigi','kong']

To remove all the elements after an index:

var removedElement = array.splice(index, array.length)

removedElement will have the list of elements removed from the array.

> example:

let index = 2;
var removedElement = array.splice(2, array.length); 
removedElement = ["kong"];
array = ["mario", "luigi"];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStarkersView Question on Stackoverflow
Solution 1 - JavascriptdandavisView Answer on Stackoverflow
Solution 2 - Javascriptsha-1View Answer on Stackoverflow
Solution 3 - JavascriptNiño Angelo Orlanes LapuraView Answer on Stackoverflow
Solution 4 - JavascriptnoobView Answer on Stackoverflow
Solution 5 - JavascriptYagnesh KhamarView Answer on Stackoverflow