JavaScript使用pop方法移除数组最后一个元素用法实例


本文实例讲述了JavaScript使用pop方法移除数组最后一个元素的用法。分享给大家供大家参考。具体如下:

下面的代码演示了JS数组的pop方法,可以用来移除数组的最后一个元素,实际上就是把数组当成堆栈使用

<!DOCTYPE html>
<html>
<body>
<p id="demo">
Click the button to remove the last array element.
</p>
<button onclick="myFunction()">Try it</button>
<script>
var fruits = ["Banana","Orange","Apple","Mango"];
function myFunction()
{
fruits.pop();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>

运行结果如下:

Banana,Orange,Apple

希望本文所述对大家的javascript程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3