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

How to delete array array elements in JavaScript

coldplay.xixi
coldplay.xixiOriginal
2021-04-08 17:56:217347browse

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);

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]);
}

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!

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