In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what are the methods in the javascript array. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1.concat ()
The concat () method is used to join two or more arrays. Method does not change the existing array, but returns a new array containing the values of the joined array.
Var str1 = [1, str1.concat, 2, 3, 4, 5, 5, 5, 6] var str2 = [] console.log (str1.concat (str2)); / / [1, 2, 3, 4, 5, 6, 4, 5, 6, 6, 5, 5, 5, 6, 2.copyWithin ()
CopyWithin () overwrites a portion of the data from the array to another location in the array. The array is modified, but not the length of the array.
Var str1 = [1 str1.copyWithin (2)) / / all elements after index 3 are omitted, all elements after index 3 are omitted, all elements after index 3 are copied and replaced by console.log from index 1 to 1 (excluding end position). / / [1,4,5,6,5,6] 3.Object.entries ()
Returns an array with elements corresponding to enumerable attribute key-value pairs found directly on object
Var str3 = {100: 'asides, 2:' baked, 7:'c'}; console.log (Object.entries (str3)); / / ['2percent,' b'] ['7percent,' c'] ['100percent,' a'] 4.every ()
The every () method is used to determine whether all elements of the array meet the specified conditions.
Var str1 = [1 return value 2, 3 value,index 4, 5 5, 6] / / create a function to determine whether all elements of the array satisfy the specified condition or not. Console.log (str1.every (checkAdult)); / / true5.fill ()
The fill () method is used to replace all elements of the array with static elements.
Var str1 = [1, 2, 3, 4, 5, 6] console.log (str1.fill (1)); / / [1, 1, 1, 1, 1] 6.filter ()
It is used to filter out some elements of Array and return the remaining elements.
Var str1 = [1 return (x) {return x > 2})] console.log (function (x) {5)); / / [3 7.find () and findIndex ()
Find () returns the value of the first element in the array that passes the test.
FindIndex () returns the index of the first element in the array that passes the test.
Var str1 = [console.log (function (x) {return x > 2}); / / 3console.log (str1.findIndex (function (x) {return x > 2})); / / 28.forEach ()
ForEach () calls the function for each array element.
Var str1 = [1, console.log 2, 3, 4, 5, 6] str1.forEach (x = > console.log (x)); / / output 1234569.from ()
Create an array of objects.
Console.log (Array.from ('string')); / / [' Array.from, 'tasking,' ringing, 'iTunes,' nasty,'g'] 10.includes ()
Includes () checks whether the array contains the specified element. The second parameter can specify where to start the detection.
Var str1 = [1 true11.indexOf 2 3 7 4 5 5 6] console.log (str1.includes (2)); / / true11.indexOf () and lastIndexOf ()
IndexOf () searches the array for elements and returns its index. The second parameter specifies where to start the search
LastIndexOf () starts searching for elements at the end of the array and returns its index. The second parameter specifies where to start the search
Var str1 = [console.log (str1.indexOf (2)); / / 1console.log (str1.lastIndexOf (2)); / / 112.isArray ()
It can be used to determine whether it is an array object.
Var str1 = [console.log (Array.isArray (str1)); / / true13.join ()
Concatenates all elements of the array into a string. The element is separated by the specified delimiter. The default delimiter is comma (,).
Var str2 = [const a = str2.join () console.log (a); / / abcde14.keys (a) console.log (a); / / abcde14.keys ()
Returns an array whose attributes are the keys corresponding to the array
Var str3 = {100: 'axed, 2:' baked, 7:'c'}; console.log (Object.keys (str3)); / / ['2percent,' 7percent, '100'] / / string returns the index console.log (Object.keys ("string")); / / [' 0,1,'2,'3,'4,'5'] 15.map ()
Call the result of the function for each array element to create a new array.
Var str1 = [const 2, 3, 4, 5 and 6] const a = str1.map (item = > item * 10) console.log (a); / / [10,20,30,40,50,60] 16.pop ()
Deletes the last element of the array and returns it. Method changes the length of the array.
Var str1 = console.log (str1.pop (), str1); / / 6, 17.push ()
Adds a new element to the end of the array and returns the new length. Method changes the length of the array.
Var str1 = [console.log (7), str1] console.log (str1.push (7), str1); / / 7, [1, 2, 4, 5, 6, 7] 18.reduce () and reduceRight ()
Reduce () execution order is from left to right reduceRight () execution order is from right to left
The callback function is executed for each element of the array in turn, which is generally used for accumulators.
There are four parameters, the first of which is the initial value or the return value at the end of the calculation. The second is the current element. The third is the index of the current element. The fourth is the array object to which the current element belongs.
Initiavalue: when the array is empty, you can set the initial value passed to the function and put it after the object
Var str1 = [1arr.reduce (function (total) {return total}, 0)) / / the use of console.log (str1.reduce (function (total,num,index,add) {return total+num})) of accumulators; / / the use of var arr= [] console.log (function (total) {return total}, 0)) of accumulators
Reverses the order of elements in the array.
Var str1 = [1, 2, 3, 4, 5, 6] console.log (str1.reverse ()); / / [6, 5, 4, 3, 2, 1] 20.shift ()
Deletes the first element of the array and returns it.
Var str1 = [console.log (str1.shift (), str1); / / 1, [2Jing 3jue 4je 5jue 6] 21.slice ()
Select part of the array and return the new array.
Var str1 = [1 str1.slice (1)] / / two parameters start at 1 and end at 3, excluding the ending position, forming a new array console.log (str1.slice (1Magol 3)); / / one parameter represents all elements starting with index 1 to form a new array console.log (str1.slice (1)); / / [2pyrm3 (1)); / / [2pyrm (1))
Check whether any element in the array passes the test. Similar to every ()
Some will no longer execute if it encounters true. Returns false if both are false. The original array will not be changed
Var str1 = [1 return item 2 3 str1.some 5 6] function some (item,index,array) {return item > 5} console.log (str1.some (some)); / truefunction some (item,index,array) {return item > 7} console.log (str1.some (some)); / / false23.sort ()
If srot () takes no arguments, it sorts the elements in the array alphabetically, that is, in the order in which the characters are encoded.
If the number type is not sorted normally from smallest to largest, it is more like sorting by the size of the first digit of the element.
If you want to sort from big to small, from small to big. This can be achieved by comparing the size of the value.
From small to large, the meaning of a murb: a murb is greater than 0, which means that an is large, then put an after. If a color b is less than 0, it means that b is large, then b is later. If amurb equals 0, it remains the same. From big to small is the opposite of the above, do not say (do not want to write, the virtue of programmers is lazy)
As for why it is a-b _ r _ r _ b-a, you can read this article https://www.yisu.com/article/238275.htm
Var arr1 = ['asides,' dudes, 'canals,' b']; arr.sort (); / ['asides,' bads, 'cations,' d'] var arr2 = [10,5,40,25,100,1]; arr2.sort (); / / [1,10,100,25,40,5] / sort from small to large var arr2 = [10,5,40,25,100,1] Arr2.sort ((console.log b)) console.log (arr2); / / [1,5,10,25,40,100] / / sort from large to small var arr2 = [10,5,40,25,100,1]; arr2.sort ((amerb) = > bmura) console.log (arr2); / / [100,40,25,10,5,1] 24.splice ()
Add / remove elements from the array.
Var str1 = [1 0'console.log 2 3)] / / delete 2 console.log (str1.splice (1 minute 2), str1) starting from index 1; / / [2 meme 3], [1, 4 parry 5 pension6] / / add, from index 1 to index 2, replace it with '0'console.log (1, 2), str1) / / [2,3], [1,'0,'0, 4, 5, 6] 25.toString ()
Converts the array to a string and returns the result.
Var str1 = [1, str1.toString (), str1); / / 1, 2, 4, 4, 5, 6, 5, 5, 6, 5, 5, 6, 5, 5, 6, 5, 5, 6, 5, 5, 6, 5, 5, 6, 6, 5, 5, 6, 26.unshift.
Adds a new element to the beginning of the array and returns a new array length value.
Var str1 = [1 str1.unshift (0), str1] console.log (0), / / 7, [0,1 mine2, 3d4 and 5] 27.valueOf ()
The valueOf () method returns itself.
Var str1 = [1, str1.valueOf 2, 3, 4, 5, 6] str1.push (7) console.log (str1.valueOf (), str1) / [1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7] this is the end of the article on "what are the methods in the javascript array". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please 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.