數組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方法