Javascript 删除索引后的所有项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26568536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove all items after an index
提问by Starkers
I have an array:
我有一个数组:
array = ['mario','luigi','kong']
I call its splicefunction to remove all items before an index:
我调用它的splice函数来删除索引之前的所有项目:
array.splice(1) //-> ['luigi','kong']
I'm just wondering if there is a function similar to spliceto remove all items after an index:
我只是想知道是否有类似于splice的函数来删除索引后的所有项目:
pseudo code
伪代码
array.mirrorsplice(1) //-> ['mario','luigi']
回答by dandavis
Use Array.lengthto set a new size for an array, which is faster than Array.spliceto mutate:
使用Array.length设置新的大小的数组,这是比快Array.splice变异:
var array = ['mario','luigi','kong', 1, 3, 6, 8];
array.length=2;
alert(array); // shows "mario,luigi";
Why is it faster? Because .splicehas to create a new array containing all the removed items, whereas .lengthcreates nothing and "returns" a number instead of a new array.
为什么它更快?因为.splice必须创建一个包含所有已删除项目的新数组,而.length什么也不创建并“返回”一个数字而不是一个新数组。
To address .spliceusage, you can feed it a negative index, along with a huge number to chop off the end of an array:
为了解决.splice使用问题,你可以给它一个负索引,以及一个巨大的数字来切断数组的末尾:
var array = ['mario','luigi','kong'];
array.splice(-1, 9e9);
alert(array); // shows "mario,luigi";
回答by sha-1
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 splicewhich is known as slice. Like following:
虽然为数组长度分配一个较短的值(如@dandavis 所说)是从数组中删除尾随元素的最快和最简单的方法,但您也可以使用类似的方法来做到这一点,例如splice,称为slice。像下面这样:
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 slicemethod. To understand 'why', here are the major distinction between sliceand splicemethod:
如您所见,您需要存储slice方法的返回值。要理解“为什么”,以下是slice和splice方法之间的主要区别:
- The
splice()method returns the removed item(s) in an array andslice()method returns the selected element(s) in an array, as a new array object. - The
splice()method changes the original array andslice()method doesn't change the original array.
- 该
splice()方法返回数组中删除的项目,slice()方法返回数组中的选定元素,作为新的数组对象。 - 该
splice()方法更改原始数组,slice()方法不更改原始数组。
回答by Ni?o Angelo Orlanes Lapura
To removeall 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 splicestarts removing at the index.
您需要 +1,因为splice开始在索引处删除。
回答by Yagnesh Khamar
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)
removedElementwill have the list of elements removed from the array.
removeElement将从数组中删除元素列表。
example:
例子:
let index = 2;
var removedElement = array.splice(2, array.length);
removedElement = ["kong"];
array = ["mario", "luigi"];

