Home > Web Front-end > Front-end Q&A > How to delete an item in an array in es6

How to delete an item in an array in es6

青灯夜游
Release: 2023-01-16 16:40:49
Original
6157 people have browsed it

Delete method: 1. Use splice() to delete any element based on the subscript, the syntax is "arr.splice(index,1)"; 2. Use the delete keyword to delete any element based on the subscript, the syntax "delete arr[index];"; 3. Use shift() to delete the first element, the syntax is "array.shift()"; 4. Use pop() to delete the last element, the syntax is "array.pop()"; 5. Use length to delete the last element.

How to delete an item in an array in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

In es6, if you want to delete any item (an element) in the array, you can use the array's splice() method or delete keyword.

Method 1. splice: After deletion, the following elements will be automatically filled in to the front

arr.splice(index, 1) 
Copy after login

Example: Now there are Array arr=['a','b','c','d']

arr.splice(1, 1); //结果arr=['a','c','d'](下标1开始,删除1个)
Copy after login

Note:

In the above code, we should pay attention to that if we want to modify the value of arr, just operate arr directly like this, instead of writing arr= arr.splice(1,1), because the splice() method The return value is the deleted element.

Added:

  • spice added:
arr.splice(1,0,'str'); //结果arr=['a','str','b','c','d']
Copy after login
  • spice replaced:
arr.splice(1,1,'str'); //结果arr=['a','str','c','d']
Copy after login
  • spice Replacement 2:
arr.splice(1,2,'str'); //结果arr=['a','str','d'](就是说:下标1开始2个换成1个“str”)
Copy after login
  • spice Delete multiple:
arr.splice(1,2); //结果arr=['a','d']
Copy after login

Method 2, delete: after deletion , the subscript position element is undefined

delete arr[index];
Copy after login

Example:

delete arr[1];
Copy after login

How to delete an item in an array in es6

The gap element can be read and written, and the length attribute does not exclude gaps. , the return value of the empty element bit is undefined

console.log(arr[1]);
Copy after login

How to delete an item in an array in es6

. If you just want to delete the first or last element, you can also use shift(), pop(), length method.

Method 3: Use the shift() function to delete the first array element

array.shift() function can remove the first element of the array elements are removed from it and the value of the first element is returned; then all remaining elements are shifted forward by 1 position to fill the gap at the head of the array.

var a = [1,2,3,4,5,6,7,8];  //定义数组
a.shift();
console.log(a);
Copy after login

The output result is:

How to delete an item in an array in es6

Note: The shift() method will change the original array and modify the length of the array!

Method 4: Use pop() to delete the last array element

array.pop() method can delete the last element in the array , and returns the deleted element.

var a = [1,2,3,4,5,6,7,8];  //定义数组
a.pop();
console.log(a)
Copy after login

The output result is:

How to delete an item in an array in es6

Note: The pop() method will also change the original array and modify the length of the array!

Method 5: Use the length attribute to delete the last array element

The length attribute of the array is generally used to set or return the elements in the array Number, that is, setting or returning the array length.

We can take advantage of the feature of setting the length of the array and set the length attribute to be smaller than the original length to delete one or more elements from the end of the array; if the value is set to 0, the entire array will be deleted. That is, clear the array!

Syntax:Array object.length=original array length-1;

Example:

var a = [1,2,3,4,5,6,7,8];  //定义数组
console.log(a)
a.length=7;
console.log(a)
Copy after login

How to delete an item in an array in es6

[Recommended learning: javascript advanced tutorial]

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