How to use pop() and shift() to delete array elements in js? (code example)

青灯夜游
Release: 2018-10-22 13:38:36
Original
5262 people have browsed it

How to delete array elements from js array? This article will introduce to you how to delete elements from js arrays, so that you can understand how to use pop() and shift() to delete elements from js arrays (one-dimensional). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Method 1: Use pop() method to delete elements from js array

The pop() method can delete the last element of the array. and returns the deleted element value. Note: The length of the array will change, minus 1.

Note:

If the array is already empty, pop() does not change the array and returns an undefined value.

Code example: delete the rabbit at the end of the animal array

    

数组:cat,elephant,tiger,rabbit;
数组长度为:4

Copy after login

Rendering:

How to use pop() and shift() to delete array elements in js? (code example)

##Note: Array.length Returns the length of the array.


Method 2: Use the shift() method to delete elements from the js array

The shift() method can remove the first element of the array Element is deleted and the deleted element value is returned. Note: The length of the array will change, minus 1.

Code example:

    

数组:cat,elephant,tiger,rabbit;
数组长度为:4

Copy after login
Code example:


How to use pop() and shift() to delete array elements in js? (code example)##The above is what this article introduces about adding in a js array The three methods of elements are pop() and shift(). Which method to choose at work depends on work needs and personal habits. Newbies can try it themselves to deepen their understanding. I hope this article can help you! For more related tutorials, please visit:

JavaScript Video Tutorial

,jQuery Video Tutorial,bootstrap Video Tutorial!

The above is the detailed content of How to use pop() and shift() to delete array elements in js? (code example). 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!