In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about whether javascript can take the value of an array. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Javascript can take the value of the array, get the value of the method: 1, the use of "array variable name [subscript value]" statement; 2, the use of slice () function, syntax "arr.slice (subscript, number of elements)"; 3, the use of splice () function, syntax "arr.splice (subscript, number of elements)".
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
The method of getting Array values by javascript
Method 1: use the form of array variable name [subscript value]
Var fruits = ['Apple', 'banana', 'durian', 'orange', 'jackfruit', 'pear']; console.log (fruits [2])
Method 2: use the slice () function
The slice () method is sufficient to intercept the elements of the specified section of the array and return the subarray. The method contains two parameters that specify the subscript at the start and end of the intercepted subarray, respectively.
Var a = [1 var b = a.slice (2)]; / / all elements console.log (b) before the third element to the sixth element are intercepted; / / all elements before the sixth element are truncated; / / return [3Jing 4jue 5]
When using this method, you should pay attention to the following problems.
1) the first parameter specifies the starting subscript position, including the element specified by the value, and the second parameter specifies the end position, excluding the specified element.
2) the parameters of this method can be set freely. If no argument is passed, no action is performed; if only one parameter is specified, all elements at the end of the array are intercepted from the subscript position specified by that parameter value.
Var b = a.slice (2); / / intercept the third element in the array, and all subsequent elements console.log (b); / / return [3meme 4meme 5]
3) when the parameter is negative, it means positioning from right to left, that is, reciprocal positioning, instead of positioning in positive order (from left to right), but the order of values is still from left to right.
Var b = a.slice (- 4); / / intercepts the element console.log (b) in front of the penultimate element from the penultimate element to the penultimate element; / / returns [2mem3]
If the starting subscript value is greater than or equal to the ending subscript value, no action is performed.
Var b = a.slice (- 2); / / intercepts the element console.log (b) before the penultimate element from the penultimate element to the penultimate element; / / returns an empty set
The above example shows that when intercepting, the array always operates in left-to-right order, not in reverse from right to left.
4) when the starting parameter value is greater than or equal to the length attribute value, no action will be performed and an empty array will be returned. If the second parameter value is greater than the length attribute value, it will be considered as the length attribute value.
Var b = a.slice (3jue 10); / / intercept the fourth element until all subsequent elements console.log (b); / / return [4meme 5]
5) the slice () method returns a portion of the array (a subarray), but does not modify the original array. The splice () method is intercepted based on the original array. If you want to intercept based on the original array instead of intercepting it as a new array, you can only use the splice () method.
Method 3: use the splice () function
The splice () method can add elements, delete elements, or intercept array fragments. When you delete an element, the deleted array fragment is returned, so you can use the splice () method to intercept the array fragment.
The return value executed by the splice () method is the deleted subarray.
Var a = [1 var 2, 3 a.splice 4]; / / define the array console.log b = a.splice (2); / / delete console.log (b) starting from the third element; / / the deleted subarray is [1Jing 2]
If the element is not deleted, an empty array is returned.
Var b = a.splice (2 Array 0); / / do not perform delete operation console.log (b.constructor = = Array); / / return true, which means it is an empty array. Thank you for reading! This is the end of this article on whether javascript can take the value of an array. 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, you can share it for more people to see!
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.