In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to delete items in javascript array". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to delete items in javascript array" can help you solve the problem.
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, 3, 4, 5]; a.splice (2); console.log (a); that's all for "how to delete items in the javascript array". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.