Home >Web Front-end >JS Tutorial >How to delete array array elements in JavaScript
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.
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!