In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how es6 adds an element to an array". In daily operation, I believe many people have doubts about how es6 adds an element to an array. Xiaobian consulted all kinds of data and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "how es6 adds an element to an array". Next, please follow the editor to study!
Three methods: 1, with unshift (), you can insert an element at the beginning, syntax "array object. Unshift (element)"; 2, with push (), you can insert an element in the tail, syntax "array object. Push (element)"; 3, with concat (), syntax "array .concat (element)".
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
Three ways for es6 to add an element to an array
1. Use the unshift () function
The unshift () function inserts elements at the beginning of an 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 (1); / / add an element console.log (a); / / return [1J0]
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]; / / define the array console.log (a); / / return [0] a.push (2); / / add an element console.log (a); / / return [0Pie2]
3. Use the concat () function
The concat () method can concatenate two or more arrays, adding the elements of one or more arrays as parameters to the end of the specified array.
You can also insert a given element or elements, adding all passed parameters sequentially to the end of the array.
Var a = [1Jing 2 Jing 3 Jing 4 Jing 5]; / / define the array console.log (a); var b = a.concat (6); / / join one element console.log (b) for the array a
The output is as follows:
Note: the concat () method creates and returns a new array instead of adding new elements to the original, but the unshift () method adds elements to the original array.
At this point, the study on "how es6 adds an element to an array" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.