Get the App
SLTechnology News&Howtos  ›  Development  › 

How to delete the element of an array by javascript

Shulou Source: shulou.com Published: 2022-06-01 19:28:16 11月01日 Update

This article mainly explains "javascript how to delete the elements of the array", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "javascript how to delete the elements of the array" bar!

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

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

The method of deleting the element of an array by javascript

1. Using splice () method

A splice () method is provided in the Array object of JavaScript to perform specific operations on the array. Splice () is probably the most powerful array method, and it can be used in many ways. I only introduce how to delete array elements here. When deleting an array element, it can delete any number of items by specifying only two parameters: the location of the first item to delete and the number of items to delete.

Syntax for deleting an element:

Array.splice (index,howmany)

The first parameter, index, can specify the location of the starting subscript (that is, where to start deleting the element)

The second parameter, howmany, specifies the number of elements that should be deleted (that is, one or more elements to be deleted).

Let's take a look at the following examples:

Var arr=; console.log (arr); arr.splice (1pm 1); console.log (arr)

You can see that you delete 1 element from the location of subscript 1 (the second element of the array) using arr.splice (1), that is, delete arr [1], so the output is as follows:

2. Use the delete keyword

JavaScript provides a delete keyword to delete (clear) array elements.

Var colors = ["red", "blue", "grey", "green"]; delete colors [0]; console.log (colors); / / [undefined, "blue", "grey", "green"]

Note that the array length does not change after the element is deleted using delete, except that the deleted element is set to undefined.

Thank you for reading, the above is the content of "javascript how to delete the elements of the array", after the study of this article, I believe you have a deeper understanding of the problem of how to delete the elements of the array by javascript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Elements arrays positions methods keys keywords parameters syntax learning subscript content index the strongest that is examples just multiple objects that is ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Microsoft vpn NVidia