search
HomeCommon ProblemWhat are the methods to delete array elements in JavaScript?

What are the methods to delete array elements in JavaScript?

Dec 07, 2023 pm 02:42 PM
javascriptArray element deletion

Methods to delete array elements in JavaScript: 1. splice() method; 2. filter() method; 3. delete operator; 4. pop() method; 5. shift() method; 6. Use the length attribute. Detailed introduction: 1. The splice() method can add or delete elements of the array at any position. It accepts two parameters, the index of the array to be changed and the number of elements to be deleted; 2. The filter() method creates a new array, elements in a new array, etc.

What are the methods to delete array elements in JavaScript?

#In JavaScript, there are multiple ways to delete elements from an array. The following are some commonly used methods:

1. splice() method:

The splice() method can add or delete elements of the array at any position. It accepts two parameters: the index of the array to start changing (starting at 0) and the number of elements to remove.

For example, to remove 3 elements starting at index 2, you would do this:

let arr = [1, 2, 3, 4, 5];  
arr.splice(2, 3); // 删除元素 3, 4, 5  
console.log(arr); // 输出: [1, 2]

You can also use the splice() method to add elements. For example, if you want to add 3 elements starting at index 2 (for example, 7, 8, 9), you can do this:

let arr = [1, 2, 3, 4, 5];  
arr.splice(2, 0, 7, 8, 9); // 添加元素 7, 8, 9  
console.log(arr); // 输出: [1, 2, 7, 8, 9]

2, filter() method:

The filter() method creates a new array. The elements in the new array are checked by checking all elements in the specified array that meet the conditions.

For example, if you want to delete all even numbers in the array, you can do this:

let arr = [1, 2, 3, 4, 5];  
arr = arr.filter(item => item % 2 !== 0); // 删除所有偶数  
console.log(arr); // 输出: [1, 3, 5]

3. delete operator:

delete operation will delete the last element of the array. However, it does not change the length of the array. Therefore, if you try to access the deleted element, you will get undefined.

For example:

let arr = [1, 2, 3, 4, 5];  
delete arr[2]; // 删除元素 3  
console.log(arr[2]); // 输出: undefined

It should be noted that using the delete operator multiple times will destroy the continuity of the array, causing subsequent elements to move forward to fill the gaps. Therefore, it is generally not recommended to use the delete operator to delete elements from an array.

4. pop() method:

The pop() method deletes and returns the last element of the array. Similar to the delete operator, it does not change the length of the array. Therefore, if you try to access the deleted element, you will get undefined.

For example:

let arr = [1, 2, 3, 4, 5];  
arr.pop(); // 删除元素 5  
console.log(arr[4]); // 输出: undefined

Similar to the delete operator, using the pop() method multiple times will destroy the continuity of the array.

5. shift() method:

The shift() method deletes and returns the first element of the array. Similar to the pop() method, it does not change the length of the array. Therefore, if you try to access the deleted element, you will get undefined. In addition, similar to the pop() method, using the shift() method multiple times will destroy the continuity of the array.

6. Use the length property:

You can truncate the array by setting the length property of the array. For example:

let arr = [1, 2, 3, 4, 5];  
arr.length = 3; // 删除元素 4, 5  
console.log(arr); // 输出: [1, 2, 3]

The above is the detailed content of What are the methods to delete array elements in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment