Home > Web Front-end > Front-end Q&A > How to delete the last element from es6 array

How to delete the last element from es6 array

青灯夜游
Release: 2022-10-27 18:43:19
Original
6006 people have browsed it

4 deletion methods: 1. Use the length attribute to delete the last array element, the syntax "array.length=original array length-1;"; 2. Use the delete operator to delete the last array element, the syntax " delete array name [array length-1];"; 3. Use pop() to delete the last array element, the syntax is "array.pop()"; 4. Use splice() to delete the last array element, the syntax is "array.splice (-1,1)".

How to delete the last element from es6 array

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

Several ways to delete the last element of an es6 array

Method 1: Use the length attribute to delete the last array The length attribute of the element

array is generally used to set or return the number of elements in the array, that is, to set or return the array length.

We can use the feature of setting the length of the array and set the length attribute to be smaller than the original length to delete an array element from the end of the array.

Syntax: array.length=original array length-1;

Example:

var a = [1,2,3,4,5,6,7,8];  //定义数组
console.log(a)
a.length=7;
console.log(a)
Copy after login

How to delete the last element from es6 array

Method 2: Use the delete operator to delete the last array element

We all know that each element in the array has a serial number. This serial number starts from 0 and is called an index. . According to this array subscript, we can use the form array name[subscript] to access the element of the specified subscript.

After accessing the specified element, you can use the delete operator to delete the element, but the length of the array will not change; the deleted element will change For empty elements,

only need to use the subscript to access the last array element, and use the delete operator to delete the element.

Syntax:delete array name[array length-1];

Example:

var arr=new Array("香蕉","苹果","梨子","橙子","橘子","榴莲");
console.log(arr);
delete arr[arr.length-1];  //删除最后一个数组元素
console.log(arr);
Copy after login

How to delete the last element from es6 array

Method 3: Use pop() to delete the last array element

The pop() method is used to delete the last element of the array and return the deleted element.

Syntax: array.pop()

Note: This method changes the length of the array!

Example:

var arr=new Array("香蕉","苹果","梨子","橙子","橘子","榴莲");
console.log(arr);
arr.pop();
console.log(arr);
Copy after login

How to delete the last element from es6 array

Method 4: Use splice() to delete the last array element

Use splice () can delete one or more elements starting from the specified subscript position.

Syntax for deleting elements:

array.splice(index,howmany)
Copy after login
  • The first parameter index can specify the starting subscript position (that is, the position where the element is deleted);

  • The second parameter howmany specifies the number of elements that should be deleted (that is, one or more elements that need to be deleted).

You only need to set the second parameter howmany to 1, that is, delete an element at any index position.

If you want to delete the last array element, set the value of the first parameter index of this method to a negative number (-1), then it will be positioned from the right side of the array to the left according to the absolute value; and The value of the second parameter howmany must be equal to the absolute value of index, which is also set to 1 or can be omitted.

Example:

var arr=new Array("香蕉","苹果","梨子","橙子","橘子","榴莲",2,3);
console.log(arr);
arr.splice(-1,1);
console.log(arr);
Copy after login

How to delete the last element from es6 array

[Related recommendations: javascript video tutorial, Programming video]

The above is the detailed content of How to delete the last element from es6 array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template