How to clear all elements of es6 array

青灯夜游
Release: 2022-03-09 18:15:04
Original
3800 people have browsed it

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;".

How to clear all elements of es6 array

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);
Copy after login

Output result:

How to clear all elements of es6 array

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);
Copy after login

How to clear all elements of es6 array

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);
Copy after login

How to clear all elements of es6 array

[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!

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