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

How to delete an object in an array in es6

WBOY
Release: 2022-05-23 15:38:21
Original
5095 people have browsed it

Method: 1. Use the splice() method, the syntax is "splice (start subscript to be deleted, length to be deleted)", this method will change the original array; 2. Use delete, the syntax For "delete arr [subscript of the object to be deleted]", this method will set the specified value to undefined.

How to delete an object in an array in es6

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

How to delete an object in the array in es6

1. splice method

splice(index,len,[item]) Note: This Method changes the original array.

splice has 3 parameters, it can also be used to replace/delete/add one or several values ​​in the array

index: array starting subscript len: replacement/delete length item : Replacement value. If the operation is deleted, the item will be empty.

For example: arr = ['a','b','c','d']

Delete---- item Not set

arr.splice(1,1) //[‘a‘,‘c‘,‘d‘] 删除起始下标为1,长度为1的一个值,len设置的1,如果为0,则数组不变
arr.splice(1,2) //[‘a‘,‘d‘] 删除起始下标为1,长度为2的一个值,len设置的2
Copy after login

2, delete

delete After deleting the element in the array, the subscripted value will be set to undefined, and the length of the array will not Change

For example:

delete arr[1] //[‘a‘, ,‘c‘,‘d‘]
Copy after login

There are two commas in the middle, the length of the array remains unchanged, and one item is undefined

[Related recommendations: javascript video tutorialwebfrontend

The above is the detailed content of How to delete an object in an array in es6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
es6
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