How to delete the last element in an array in JavaScript

PHPz
Release: 2023-04-17 11:46:21
Original
10459 people have browsed it

During the development process, we sometimes need to operate elements in the array. One of the common operations is to delete an element in the array. This article will introduce how to use the splice() method in JavaScript to delete the last element in an array.

  1. splice() method
    The splice() method in JavaScript is used to add or remove elements from an array. It can accept three parameters, namely: the starting position of deletion, the number of deletions, and the elements to be added.

The syntax is as follows:

array.splice(start, deleteCount, item1, item2, ...)
Copy after login

Among them:

  • start: required. Indicates the starting position of deletion. If it is a negative number, it represents the position from the end of the array.
  • deleteCount: Optional. Indicates the number of elements to be deleted. If it is 0, the element is not deleted.
  • item1, item2, ...: optional. Indicates the elements to be added to the array, starting from the start position.

The splice() method will return an array consisting of the deleted elements. If only one element is deleted, an array containing only this element is returned; if no elements are deleted, an empty array is returned.

  1. Delete the last element
    To delete the last element in the array, we only need to set the starting position of the splice() method to the length of the array minus 1, and add the element to be deleted The number is set to 1. Because the length of the array starts counting from 1, and the starting position of the splice() method starts counting from 0, the starting position of deleting the last element is the length of the array minus 1.

The sample code is as follows:

// 定义一个数组
var arr = ["apple", "banana", "orange", "pear"];

// 删除最后一个元素
arr.splice(arr.length - 1, 1);

// 输出数组
console.log(arr); // ["apple", "banana", "orange"]
Copy after login

In the above code, we define an array arr containing 4 elements. Then, we call the splice() method, set the starting position to arr.length - 1, and set the number of elements to be deleted to 1 to achieve the function of deleting the last element. Finally, the array arr is output, and we can see that the last element has been deleted.

  1. Summary
    This article explains how to use the splice() method in JavaScript to delete the last element in an array. This is a simple and common operation, which is often used in uniapp development. I hope this article can help readers better understand the related knowledge of array operations.

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

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