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 > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the practical skills of Array in JavaScript". In daily operation, I believe many people have doubts about the practical skills of Array in JavaScript. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the practical skills of Array in JavaScript?" Next, please follow the editor to study!
About Array
The creation of Array is flexible, either using the Array constructor or directly creating arrays of literals.
Var arr = new Array (); / / [] var brr = Array (); / / [] the two are equivalent var arr = Array (3); / / [] arr.length; / / 3 empty array of length 3 var arr = Array (22Yue33, qq, Object); / / Ibid.
Array is the built-in object of JavaScript, yes, although it is an array, it is also an object!
Using typeof judgment returns Object! The Array.isArray method can judge its type more accurately.
Var a = []; typeof a; / / object Array.isArray (a); / / true
Common methods
Push () method
The push method can add one or more elements to the end of the array and return the length of the changed array!
Note: ① returns the length of the array, not the array!
② this method will change the original array!
Var arr = Array (22 weibo' 33); arr.push ('weibo'); / / 5 arr / / [22 var arr 33, "qq", {}, "weibo"]
We need to use this when we want to merge two arrays
Var a = [22jue 33]; var b = [44jue 55]; Array.prototype.push.apply (a, b) / or a.push.apply (a, b) / / or a.push (44jue 55); / / the array at this time a = [22pl 33pr 44jue 55]
Note that it can't be written like this!
A.push (b); a; / / [22, 22, 33, [44, 55]] a.temperth; / / 3! Console.log (a); / / [22, 33, Array [2]]
Writing it directly as a.push (b) will add b as an element to a, but it won't get the desired effect!
If you now have two more arrays of objects that need to be merged, like this:
Var a = [{name: 'Stark', value:' Ironman'}, {name: 'Cap', value:' Oldman'}]; var b = [{name: 'Jerry', email:' Jerry@qq.com'}, {name: 'Lory', email:' Lory@163.com'}, {name: 'susan', email:' susan@gmail.com'}]; / / misspelled a.push (b) / / 3 console.log (a); / / [Object, Object, Array [3]] / / correct a.push.apply (A.B); / / 5 console.log (a); / / [Object, Object]
Pop () method
In contrast to push, delete an element in the array * * and return the deleted element:
Var a = ['qq',' weibo', 'weixin']; a.pop (); / /' weixin' a; / / ['qq',' weibo']
Join () method
The array is separated according to the corresponding parameters and returned as a string. If the parameter is empty, it is separated by','. This method does not change the original array:
Var a = [1 var 2 a.join 3 4]; a.join ('') / /'12 34 'a.join (' |') / / "1 | 2 | 3 | 4" var b = a.join () / / "1 console.log (a); / / [1, 2, 2, 3, 4] console.log (b); / /" 1, 2, 3, 3, 4 "
Concat () method
You can merge multiple arrays and return a new array, but the original array remains the same:
Var a = [22 tom', email 33]; var b = [44 tom', email 55]; var c = a.concat (b); console.log (a); / / [22 name 33] console.log (b); / / [4 name 55] console.log (c); / / [22 name: 'tom', email:' tom@example.com'}, {name: 'peter', email:' peter@163.com'}] Var b = [{name: 'Jerry', email:' Jerry@qq.com'}, {name: 'Lory', email:' Lory@dfl.com'}, {name: 'susan', value:' susan@gmail.com'}]; var c = a.concat (b); c / / [{name: 'tom', email:' tom@example.com'}, / / {name: 'peter', email:' peter@163.com'}, / / {name: 'Jerry', email:' Jerry@qq.com'}, / / {name: 'Lory', email:' Lory@dfl.com'} / / {name: 'susan', value:' susan@gmail.com'}]
Map () method
The map method calls a function on each member of the array in turn, returning a new array processed by the function, but the original array is not changed!
Var numbers = [1Jing 2jue 3]; var num = numbers.map (function (n) {/ / [2,4,6] return n * 2;}); numbers; / / [1meme 2p3]
When the function called by the map method has an argument of one, this parameter represents the current member of the array; when the arguments are three, they are
Current member elem, index index, original array itself arr
Var brr = [1,2,3] .map (function (elem, index, arr) {return elem * index;}); brr; / / [0,2,6]
The map method can also take a second argument that represents the object that this points to when the callback function executes.
Var arr = ['averse,' baked,'c']; var brr = [0,2] .map (function (e) {return this [e];}, arr) brr; / / ['averse,' c']
In an application, sometimes the map method is very convenient when using ajax technology to dynamically convert a parameter array into a url request, such as:
Var b = [{name: 'Jerry', email:' Jerry@qq.com'}, {name: 'Lory', email:' Lory@dfl.com'}, {name: 'susan', value:' susan@gmail.com'}] Var url = b. Map (function (n) {return n.name + "=" + n.email}) .join ("&"); console.log (url) / / JerryJerry=Jerry@qq.com&LoryLory=Lory@dfl.com&susansusan=susan@gmail.com then add the ip address and action and method before the url to complete the stitching of a dynamic url needed by ajax, for example: var endURL = "localhost:8080/XXXX/" + eventAction + "!" + operation + "?" + url At this point, the study of "what are the practical skills of Array in JavaScript" 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.