javascript 从数组中删除最后一项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19544452/
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 last item from array
提问by Prithviraj Mitra
I have the following array.
我有以下数组。
var arr = [1,0,2];
I would like to remove the last element i.e. 2.
我想删除最后一个元素,即 2。
I used arr.slice(-1);
but it doesn't remove the value.
我使用过,arr.slice(-1);
但它不会删除该值。
回答by Stuart Kershaw
Array.prototype.pop()by JavaScript convention.
Array.prototype.pop() 按照JavaScript 约定。
let fruit = ['apple', 'orange', 'banana', 'tomato'];
let popped = fruit.pop();
console.log(popped); // "tomato"
console.log(fruit); // ["apple", "orange", "banana"]
回答by Anton
回答by pala?н
You can do this using .slice()
method like:
您可以使用以下.slice()
方法执行此操作:
arr.slice(0, -1); // returns [1,0]
Here is a demo:
这是一个演示:
var arr = [1, 0, 2];
var newArr = arr.slice(0, -1); // returns [1,0]
console.log(newArr);
$('#div1').text('[' + arr + ']');
$('#div2').text('[' + newArr + ']');
<script src="http://code.jquery.com/jquery.min.js"></script>
<b>Original Array : </b>
<div id="div1"></div>
<br/>
<b>After slice(0, -1): </b>
<div id="div2"></div>
instead of doing :
而不是做:
arr.slice(-1); // returns [2]
Here is a demo:
这是一个演示:
var arr = [1, 0, 2];
var newArr = arr.slice(-1); // returns [2]
console.log(newArr);
$('#div1').text('[' + arr + ']');
$('#div2').text('[' + newArr + ']');
<script src="http://code.jquery.com/jquery.min.js"></script>
<b>Original Array : </b>
<div id="div1"></div>
<br/>
<b>After slice(-1): </b>
<div id="div2"></div>
Explanation:-
解释:-
Now the basic syntax of Array.prototype.slice()
or in short slice()
method is:
现在Array.prototype.slice()
或简而言之slice()
方法的基本语法是:
arr.slice([begin[, end]])
Here,
这里,
the begin
parameter is zero-based index at which extraction from an array starts. So, lets say based on above example if we do something like
该begin
参数是从零开始从数组中提取的索引。所以,让我们根据上面的例子说,如果我们做类似的事情
arr.slice(0) // returns [1,0,2]
it would return all the array elements from start of sequence from position 0 and that is [1,0,2]
. Similarly, if we do
它将从位置 0 的序列开始返回所有数组元素,即[1,0,2]
. 类似地,如果我们这样做
arr.slice(1) // returns [0,2]
it would return [0,2]
since 0 is at position 1 here and everything after that. Now, in your case you have passed a negative index i.e., -1
as the begin parameter, which indicates an offset from the end of the sequence. So, slice(-1)
in your case extracts the last one array element in the sequence and that is 2
(as we have already seen in the above demo).
它会返回,[0,2]
因为 0 位于此处的位置 1 以及之后的所有位置。现在,在您的情况下,您传递了一个负索引,即-1
作为开始参数,它表示距序列末尾的偏移量。因此,slice(-1)
在您的情况下,提取序列中的最后一个数组元素,即2
(正如我们在上面的演示中已经看到的那样)。
Now, let's talk about the end
parameter in the slice()
method syntax here. It is again a zero-based index at which extraction from an array ends. So, lets say we have a array like:-
现在,让我们end
在slice()
这里谈谈方法语法中的参数。它又是一个从零开始的索引,在该索引处从数组中提取结束。所以,假设我们有一个数组,如:-
var arr = [1, 0, 2, 5, 3, 9];
and we want to get just the 2,5,3
elements in the array. Now, position of 2
from start of the sequence is 2
and for last element 3
it is 4
. We will need to end the extraction here a position 5, as we need to get the element before that position. So, we will simply implement slice()
method here like
我们只想获取2,5,3
数组中的元素。现在,2
从序列开始的位置是2
,对于最后一个元素,3
它是4
。我们需要在位置 5 处结束提取,因为我们需要在该位置之前获取元素。所以,我们将slice()
在这里简单地实现方法,如
arr.slice(2, 5) // returns [2,5,3]
In your case, we have implemented -1
as the end parameter, so our code is like
在你的情况下,我们已经实现-1
为结束参数,所以我们的代码就像
arr.slice(0, -1) // returns [1,0]
As a negative index, end
indicates an offset from the end of the sequence. So, slice(0,-1)
extracts the first element through the second-to-last element in the sequence. So, we get the desired output. We can also do like
作为负索引,end
表示距序列末尾的偏移量。因此,slice(0,-1)
通过序列中的倒数第二个元素提取第一个元素。所以,我们得到了想要的输出。我们也可以像
arr.slice(0, 2) // returns [1,0]
we will get the same output. But, I have used -1
here as its easier to implement even for a long array like
我们将得到相同的输出。但是,我在-1
这里使用它是因为即使对于像这样的长数组也更容易实现
[0,2,3,1,2,9,3,6,3,9,1,0,2,9,0,1,1,2,3,4,7,9,1]
If you just want to remove the last element, you don't want to sit & calculate the position of last 9 here and the do like arr.slice(0, 22)
. You can then simply implement the negative index logic here & do
如果你只想删除最后一个元素,你不想坐在这里计算最后 9 的位置,然后做 like arr.slice(0, 22)
。然后你可以在这里简单地实现负索引逻辑并做
arr.slice(0, -1) // same result as arr.slice(0, 22)
Hope it helps!
希望能帮助到你!
回答by Lukas Liesis
learn by example:
通过例子学习:
let array_1 = [1,2,3,4];
let array_2 = [1,2,3,4];
let array_3 = [1,2,3,4];
array_1.splice(-1,1) // output --> [4] array_1 = [1,2,3]
array_2.slice(0,-1); // output --> [1,2,3] array_2 = [1,2,3,4]
array_3.pop(); // output --> 4 array_3 = [1,2,3]
回答by Bill Criswell
You would need to do this since slice
doesn't alter the original array.
您需要这样做,因为slice
不会改变原始数组。
arr = arr.slice(-1);
If you want to alter the original array you can use splice
:
如果要更改原始数组,可以使用splice
:
arr.splice(-1, 1);
or pop
:
或pop
:
arr.pop();
回答by Matas Vaitkevicius
I would consider .pop()
to be the most 'correct' solution, however, sometimes it might not work since you need to use array without the last element right there...
我认为.pop()
这是最“正确”的解决方案,但是,有时它可能不起作用,因为您需要使用没有最后一个元素的数组...
In such a case you might want to use the following, it will return [1,2,3]
在这种情况下,您可能想要使用以下内容,它将返回 [1,2,3]
var arr = [1,2,3,4];
console.log(arr.splice(0,arr.length-1));
while .pop()
would return 4
:
while.pop()
会返回4
:
var arr = [1,2,3,4];
console.log(arr.pop());
which might not be desirable...
这可能是不可取的......
Hope this saves you some time.
希望这可以为您节省一些时间。
回答by Krishna
You could simply use, arr.pop()
你可以简单地使用, arr.pop()
This removes the last entry of the array.
这将删除数组的最后一个条目。
var arr = [1,0,2];
var popped = arr.pop();//Now arr = [1,0] & popped = 2
回答by Wannes
Just use the following for your use case:
只需将以下内容用于您的用例:
var arr = [1,2,3,4];
arr.pop() //returns 4 as the value
arr // value 4 is removed from the **arr** array variable
Just a note. When you execute pop()
function even though the line returns the popped item the original array is effected and the popped element is removed.
只是一个注释。当您执行pop()
函数时,即使该行返回弹出的项目,原始数组也会受到影响并删除弹出的元素。
回答by Harunur Rashid
You can do it in two way using splice()
:
您可以通过两种方式使用splice()
:
arr.splice(-1,1)
arr.splice(arr.length-1,1)
arr.splice(-1,1)
arr.splice(arr.length-1,1)
splice(position_to_start_deleting, how_many_data_to_delete)
takes two parameter.
splice(position_to_start_deleting, how_many_data_to_delete)
需要两个参数。
position_to_start_deleting
: The zero based index from where to start deleting.
how_many_data_to_delete
: From indicated index, how many consecutive data should be deleted.
position_to_start_deleting
:从零开始删除的索引。
how_many_data_to_delete
: 从指示的索引中,应该删除多少连续的数据。
You can also remove the last element using pop()
as pop()
removes the last element from some array.
Use arr.pop()
您还可以使用pop()
aspop()
从某个数组中删除最后一个元素来删除最后一个元素。
利用arr.pop()