Clearing method: 1. Directly assign the value to "[]", the syntax is "arr=[];"; 2. Use splice() to delete all array elements, the syntax is "arr.splice(0,arr.length );"; 3. Use the length attribute to set the array length to 0, using the syntax "arr.length=0;".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Method to clear all elements
Method 1: Directly assign an empty array[]
var arr=new Array("香蕉","苹果","梨子","橙子","橘子","榴莲"); console.log(arr); arr=[]; console.log(arr);
Output result:
Method 2: Use splice() to delete all array elements
Only The splice() method needs to be specified to start from the first array element, and the number of elements that need to be deleted is arr.length to clear the array.
var arr=new Array("香蕉","苹果","梨子","橙子","橘子","榴莲"); console.log(arr); arr.splice(0,arr.length); console.log(arr);
Method 3: Use the length attribute to set the array length to 0
The length attribute can set or return the array length. When the value of the length attribute is less than the length of the array itself, subsequent elements in the array will be truncated; if the value of the length attribute is 0, the entire array can be cleared.
var arr=new Array("香蕉","苹果","梨子","橙子","橘子","榴莲"); console.log(arr); arr.length=0; console.log(arr);
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to clear all elements of es6 array. For more information, please follow other related articles on the PHP Chinese website!