Home > Web Front-end > JS Tutorial > How to delete array array elements in JavaScript

How to delete array array elements in JavaScript

coldplay.xixi
Release: 2023-01-04 09:35:24
Original
7404 people have browsed it

JavaScript method to delete array array elements: 1. Use [splice()] to delete, the code is [console.log(arr.splice(1, 2))]; 2. Use the delete operator to delete.

How to delete array array elements in JavaScript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

JavaScript method to delete array array elements:

1. splice() deletion:

var arr = ["犀牛前端部落", 4, "青岛市南区", "pipipi.net"];
console.log(arr.splice(1, 2));
console.log(arr);
Copy after login

The code can delete the array elements with index values ​​1 and 2.

The splice method returns a new array composed of deleted elements; this method directly operates the original array.

For the splice method, please refer to the JavaScript splice() chapter.

2. Delete with delete operator:

You can also delete specified array elements using the delete operator.

The code example is as follows:

var arr = ["犀牛前端部落", 4, "青岛市南区", "pipipi.net"];
delete arr[1];
for (var index = 0; index < arr.length; index++) {
  console.log("index:" + index+",数组值:"+arr[index]);
}
Copy after login

This operator deletes array elements. The length of the array will not change, but the element value of the corresponding index will be changed to undefined.

Related free learning recommendations: javascript video tutorial

The above is the detailed content of How to delete array array elements in JavaScript. 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