Home > Article > Web Front-end > How to delete the first element from es6 array
3 ways to delete: 1. Use shift(), the syntax is "array object.shift()". 2. Use splice() to delete an element with a starting index of 0. The syntax is "array object.splice(0,1)". 3. Use delete to delete the array element with index 0, the syntax is "delete array name[0]".
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
Multiple ways to delete the first element from an es6 array
Method 1: Use the shift() function
array.shift() function can delete the first element of the array and return the value of the first element; then shift all remaining elements forward by 1 position to fill the gap at the head of the array.
array.shift() function will change the original array
var a = [1,2,3,4,5,6,7,8]; //定义数组 console.log(a); a.shift(); console.log(a);
##Method 2: Use splice() function
The splice() function can be used in JS to delete an item in the array; the splice() method is used to add or delete elements in the array. Syntax:splice(index,len,[item])Comments: This method will change the original array. splice has 3 parameters, it can also be used to
replace/delete/addone or several values in the array
var a = [1,2,3,4,5,6,7,8]; //定义数组 console.log(a); a.splice(0,1); //删除起始下标为0,长度为1的一个值,len设置的1,如果为0,则数组不变 console.log(a);
Method 3: Use the delete keyword
Use the delete keyword to delete the array element with index 0.
deleteAfter deleting the element in the array, the subscripted value will be set to undefined, and the length of the array will not changevar a = [1,2,3,4,5,6,7,8]; //定义数组 console.log(a); delete a[0]; //删除下标为0的元素 console.log(a);
【 Related recommendations: javascript video tutorial
,web front-end
】The above is the detailed content of How to delete the first element from es6 array. For more information, please follow other related articles on the PHP Chinese website!