Home  >  Article  >  Web Front-end  >  javascript pop() 删除数组中最后的一个元素

javascript pop() 删除数组中最后的一个元素

WBOY
WBOYOriginal
2016-06-01 09:54:261541browse

pop() 方法删除一个数组中的最后的一个元素,并且返回这个元素。

pop()基本语法:

<code class="language-javascript">array.pop()</code>

 

pop()方法说明

pop 方法删除一个数组中的最后一个元素,并且把这个删除掉的元素返回给调用者。

pop 被有意设计成具有通用性,该方法可以通过 call 或 apply 方法应用于一个类数组(array-like)对象上。

 

pop()方法实例:

下面的代码首先创建了一个拥有四个元素的数组 myFish,然后删除掉它的最后一个元素。

<code class="language-javascript">var myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log("myFish before: " + myFish);

var popped = myFish.pop();

console.log("myFish after: " + myFish);
console.log("Removed this element: " + popped);</code>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn