数组shift方法
JavaScript shift()方法 (JavaScript shift() method)
shift() method is used to remove the first element of an array and returns the deleted element.
shift()方法用于删除数组的第一个元素,并返回删除的元素。
It changes the array length.
它改变了数组的长度。
Syntax:
句法:
array_name.shift();
Parameters: None
参数:无
Return value: It returns a type of value that array contains, it actual returns deleted the element.
返回值:它返回数组包含的一种类型的值,它实际返回已删除的元素。
Example:
例:
Input:
var arr1 = [10, 20, 30, 40, 50];
Function call:
arr1.shift();
Output:
20, 30, 40, 50
JavaScript Code to demonstrate example of Array.shift() method
JavaScript代码演示Array.shift()方法的示例
<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
var arr1 = [10, 20, 30, 40, 50];
var arr2 = ["Amit", "Ankur", "Akash"];
//printing arrays before the deleting elements
document.write("arr1: " + arr1 + "<br>");
document.write("arr1 length: " + arr1.length + "<br>");
document.write("arr2: " + arr2 + "<br>");
document.write("arr2 length: " + arr2.length + "<br>");
//removing element
arr1.shift();
arr1.shift();
arr2.shift();
document.write("<br>After removing...<br><br>");
document.write("arr1: " + arr1 + "<br>");
document.write("arr1 length: " + arr1.length + "<br>");
document.write("arr2: " + arr2 + "<br>");
document.write("arr2 length: " + arr2.length + "<br>");
</script>
</body>
</html>
Output
输出量
arr1: 10,20,30,40,50
arr1 length: 5
arr2: Amit,Ankur,Akash
arr2 length: 3
After removing...
arr1: 30,40,50
arr1 length: 3
arr2: Ankur,Akash
arr2 length: 2
翻译自: https://www.includehelp.com/code-snippets/array-shift-method-with-example-in-javascript.aspx
数组shift方法