Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of deleting items in javascript array

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about the method of deleting items in the javascript array. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

Delete method: 1, use pop () to delete the element at the end of the array; 2, use shift () to delete the element at the beginning of the array; 3, delete the specified subscript position element with the delete operator; 4, delete the tail one or more elements with the length attribute; 5, delete one or more elements after the specified subscript position with splice ().

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

Method 1: use the pop () method to delete elements at the end of the array

The pop () method deletes the last element in the array and returns the deleted element.

Example:

Var a = []; / / defines an array that simulates empty stack console.log (a.push (1)); / / stacks with stack values of [1] and length of 1console.log (a.push (2)); / / stacks with stack values of [1], length of 2console.log (a.pop ()); / / out of stack values of [1] and length of 1console.log (a.push (3)) / / on the stack, length is 3console.log (a.pop ()); / / out of the stack, the stack value is [1jue 3], length is 2console.log (a.pop ()); / / out, the stack value is [1], length is 1

Method 2: use the shift () method to delete the element at the beginning of the array

The shift () method deletes the first element of the array, returns it, and then moves all remaining elements forward one bit to fill the gap in the array header. If the array is empty, shift () does nothing and returns undefined.

Example:

Var a = [1 for (var i in a) {/ / traversal array var t = a.pop (); / / tail pop-up a.unshift (t * 10); / / push in the head to magnify the pushed value 10 times} console.log (a); / / return [10, 20, 30, 40, 50]

Method 3: use the delete operator

Use the delete operator to delete an array element with a specified subscript location, the deleted element is an empty element, and the length of the deleted array remains unchanged.

Var a = [1,2, true, "a", "b"]; delete a [0]; console.log (a)

Method 4: use the length attribute

Use the length attribute to delete one or more trailing elements or even empty an entire array. The length of the array will be dynamically updated after the element is deleted.

Var a = [1,2, true, "a", "b"]; a.length = 3 position console.log (a)

Method 5: use the splice () method

Use the splice () method to delete one or more array elements after the specified subscript position. This method has many parameters and functions, and the example in this section only shows how to delete array elements. The first parameter is the starting subscript position of the operation, and the second parameter specifies the number of elements to delete.

Var a = [1, 2, 3, 4, 5]; a.splice (1, 2); console.log (a)

In the splice method, the first parameter value 1 means that two elements are deleted from the second element position of the array a, and there are only three elements left after the array an is deleted.

If you pass a parameter to the splice () method, the method only performs the delete operation, the parameter value specifies the starting subscript of the deleted element (including the subscript element), and the splice () method deletes all subsequent elements.

Var a = [1, a.splice (2); console.log (a); these are all the contents of this article "how to delete items in the javascript array". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report