Get the App
SLTechnology News&Howtos  ›  Development  › 

How javascript deletes the first element of an array

Shulou Source: shulou.com Published: 2022-06-02 07:24:58 10月02日 Update

This article focuses on "how to delete the first element of an array by javascript". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how javascript deletes the first element of an array.

Javascript deletes the first element of the array: 1, use the shift () function, syntax "arr.shift ();"; 2, use the delete operator, syntax "delete arr [0];"; 3, use the splice () function, syntax "arr.splice (0Jue 1)".

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

The method of deleting the first element of an array by javascript

Method 1. Use the shift () function

The shift () function removes the element at the beginning of the array, removes the first element of the array, and returns the value of the first element, and then moves all remaining elements forward one bit to fill the gap in the array header.

Var a = [1, console.log 2, 3, 4, 5, 6, 7, 8]; / / define the array a.shift (); console.log (a)

Method 2: use the delete operator

When you delete an array element based on an array subscript using the delete operator, the array length does not change, but the deleted element becomes an empty element.

Var arr=new Array ("banana", "apple", "pear", "orange", "orange", "durian"); console.log (arr); delete arr [0]; / / delete the element with the subscript 0 (first element) console.log (arr)

Method 3: use the splice () method

Var arr=new Array ("banana", "apple", "pear", "orange", "orange", "durian"); console.log (arr); arr.splice (0Magne1); console.log (arr)

At this point, I believe you have a deeper understanding of "how to delete the first element of the array by javascript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Element array method function grammar operator operation subscript content pear durian orange orange apple banana learning practical deeper interest head Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno Xiaomi Docker Redmi