Get the App
SLTechnology News&Howtos  ›  Development  › 

How javascript deletes elements in an array that specify a subscript

Shulou Source: shulou.com Published: 2022-06-03 12:59:00 10月03日 Update

This article will explain in detail how javascript deletes the elements of the specified subscript in the array. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Javascript delete the specified subscript elements in the array method: 1, the use of array splice () method, syntax "arr.splice (index, 1)"; 2, the use of the delete keyword, syntax "delete arr [index]".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Suppose the array arr has n elements, and now delete the element with the subscript index

There are two ways:

Using the splice () method of the array

Using the delete keyword

1. Splice: after deletion, the following elements are automatically filled to the front.

Arr.splice (index, 1)

For example: there is now an array of arr= ['axiomagy', 'cantiled']

Arr.splice (1,1); / / result arr= ['a subscript 1, delete 1)

Note:

In the above code, we should note that if you want to change the value of arr, manipulating arr will change it directly, instead of writing arr= arr.splice (1jue 1), because the return value of the splice () method is the deleted element.

Add:

Increase in spice:

Arr.splice; / / result arr= ['axiaxiaozhongzhongzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanshou].

Spice replacement:

Arr.splice; / / result arr= ['axiaxiaozhongzhongzhongyuanzhongyuanzhongyuanshou']

Spice replacement 2:

Arr.splice (1 and 2); / / result arr= ['axiomagencience'] (that is, 2 subscript 1 are replaced with 1 "str")

Spice deletes multiple:

Arr.splice (1 ~ 2); / / result arr= ['axiom ~ 2']

2. Delete: after deletion, the subscript location element is undefined

Delete arr [index]

Example:

Delete arr [1]

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

Console.log (arr [1])

This is the end of the article on "how to delete the specified subscript elements in the array by javascript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Tags: Element subscript array method result middle finger article key keyword more vacancy syntax good practical code location content multiple that is that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Microsoft Linux Shulou Tech Info