In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how javascript adds elements to the array. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Add methods: 1, use the unshift () function to insert elements at the beginning of the array; 2, use the push () function to insert elements at the end of the array; 3, use the concat () function to insert elements at the end of the array; 4, use the splice () function to add elements anywhere according to the array subscript.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Javascript adds elements to the array
Method 1: use the unshift () function
Use the unshift () function to insert elements at the beginning of the array, which appends one or more parameter values to the head of the array:
Array.unshift (element 1, element 2,..., element X)
The first parameter element 1 is the new element 0 of the array, the second parameter element 2 is the new element 1, and so on, and finally returns the length of the array after the element is added.
Let's take a look at the following examples:
Var a = [0]; / / define the array console.log (a); / / return [0] a.unshift (1Power2); / / add two elements console.log (a) at one time; / / return [1line 2line 0] var a = [0]; a.unshift (1); / / add element 1a.unshift (2); / / add element 2console.log (a); / / return [2line 1p0]
You can see that we use a.unshift (1Power2) to insert two values 1 and 2 sequentially at the beginning of array a; use a.unshift (1) to insert the number 1 at the beginning of array a, and then use a.unshift (2) to insert the number 2 at the beginning (that is, before the number 1). So the output is:
Method 2: use the push () function
The push () method appends one or more parameter values to the end of the array and returns the length of the array after the element is added.
Array.push (element 1, element 2,..., element X)
Let's take a look at the following examples:
Var a = [0]; / / defines the array console.log (a); / / returns [0] a.push (1Power2); / / adds two elements console.log (a) at one time; / / returns [0Power1 var 2] var a = [0]; a.push (1); / / adds element 1a.push (2); / / adds element 2console.log (a); / / returns 0mem1p2]
You can see that we use a.push (1Magne2) to insert two values 1 and 2 sequentially at the end of array a; use a.push (1) to insert a number 1 at the end of array a, and then use a.push (2) to insert a number 2 at the tail (that is, after the value 1). So the output is:
Method 3. Use the concat () function
In addition to using the push () function, you can also use the concat () function to insert elements at the end of the array.
The concat () method can also insert a given element or elements, adding all passed parameters sequentially to the end of the array.
Var a = [1, var 2, 3, 4, 5]; / define the array var b = a.concat (6, 7, 8); / / concatenate 3 elements console.log (b) for array a; / / return [1, 2, 3, 4, 5, 6, 7, 8]
The output is as follows:
Note: the concat () method creates and returns a new array instead of adding new elements to the original.
Method 4: use the splice () function
How do you want to insert one or more elements at a specified subscript location?
The answer is: you can use the splice () method.
The first parameter index of array.splice (index,howmany,item1,.,itemX) specifies the starting subscript location; the second parameter howmany specifies the number of elements that should be deleted, and when the value is set to 0, the delete operation is not performed; this allows one or more elements to be inserted through the third and subsequent parameter item1,.,itemX.
Let's take a look at the following examples:
Var a = [1, 2, 3, 4, 5]; a.splice (1, "hello"); console.log (a)
You can see that using a.splice (1jin0, "hello") to insert an element "hello" at the position of subscript 1 (that is, after the position of the first element of the array), so the output is:
Var a = [1, hello, 4, 5]; a.splice (2, hello, "hi", 8); console.log (a)
As you can see: use a.splice (2p0, "hello", "hi", 8) to insert multiple elements "hello", "hi", 8 at the position of subscript 2 (that is, after the second element of the array), so the output is:
You can also add elements at the beginning or end of an array using the splice () method:
Var a = [1, hello 2, 3, 4, and 5]; a.splice (0, "hello"); a.splice (A., hi); console.log (a)
If you want to add elements at the beginning of the array, set the value of the first parameter index of the method to 0; if you want to add elements at the end of the array, set the value of the first parameter of the method index to the length of the array, which can be obtained using a.length.
Let's take a look at the output:
This is how the javascript shared by the editor adds elements to the array. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow 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.
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.