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 Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools