删除索引后的所有项目

我有一个数组:

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

我调用它的 拼接函数来删除所有项目 在索引之前:

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

我只是想知道是否有一个类似于 拼接的函数来删除所有的项目 在索引之后:

伪代码

array.mirrorsplice(1) //-> ['mario','luigi']
85756 次浏览

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";

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.

To remove all items after an index:

var array = ['mario','luigi','kong'],
index = 1; // your index here
array = 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.

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"];

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.