In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what are the commonly used JavaScript array methods, I hope you will gain something after reading this article, let's discuss it together!
1. Filter ()
Syntax:
Array.filter (function (currentValue,index,arr), thisValue)
Parameter description:
CurrentValue: current element object (required)
Index: the index value of the current element (optional)
Arr: the array object to which the current element belongs (optional)
ThisValue: the object is used as this callback, passed to the function, and used as the value of "this".
If thisValue is omitted, the value of "this" is "undefined" (optional)
/ / filter elements older than 10 var ages = [5,32,7,10,33 12 return currentValue 40]; var res = ages.filter (function (currentValue) {return currentValue > 10;}) console.log (res.toString ()); / / output results: 32 var res1 = ages.filter (item = > item > 10) console.log (res.toString ())
Output result:
32,33,12,40
2. ForEach ()
Syntax:
Array.forEach (function (currentValue, index, arr), thisValue)
The usage of parameters is the same as above
/ / Loop output each parameter var ages = [5, 32, 7, 10, 33, 12, 40]; ages.forEach (function (currentValue, index) {console.log ("parameter:" currentValue + "index:" + index);}) / / arrow function ages.forEach ((item, index) = > {console.log ("parameter:" item + "index:" + index);})
Take another look at the following code:
/ modify 10 to 20 var ages = [5, 32, 7, 10, 33, 12, 40]; ages.forEach (function (currentValue, index) {if (currentValue = 10) {ages [index] = 20 return} console.log (index);}) console.log (ages)
After we changed the value of 10 to 20 in the code, we added a return, but the result of the run showed that the value of index was printed seven times. This is a disadvantage of forEach, which can only be stopped at the end of the loop. Then how to solve the problem?
3. Some ()
Syntax:
Array.some (function (currentValue,index,arr), thisValue)
The usage of parameters is the same as above
/ modify 10 to 20 var ages = [5, 32, 7, 10, 33, 12, 40]; ages.some (function (currentValue, index) {if (currentValue = 10) {ages [index] = 20 return true} console.log (index);}) console.log (ages); / / modify 10 to 20 arrow function var ages = [5, 32, 7, 10, 33, 12, 40] Ages.some ((item, index) = > {if (item = 10) {ages [index] = 20 return true} console.log (index);}) console.log (ages)
The result of running in the above code will only print the value of index three times, and the deficiency of forEach () can be solved perfectly through some. It depends on your needs in the development process.
4. Every ()
Syntax:
Array.every (function (currentValue,index,arr), thisValue)
The usage of parameters is the same as above
/ / determine whether the value of each element is greater than 4 var ages = [5, 32, 7, 10, 33, 12, 40]; var res = ages.some (function (currentValue) {return currentValue > 4}) console.log (res); / / output: true / / arrow function var res = ages.some (item = > item > 4) console.log (res); 5, reduce ()
Syntax:
Array.reduce (total, currentValue, currentIndex, arr), initialValue)
Parameter description:
Total: required. The initial value, or the return value after the calculation is finished.
CurrentValue: required. Current element
CurrentIndex: optional. Index of the current element
Arr: optional. The array object to which the current element belongs.
InitialValue: optional. The initial value passed to the function
/ / calculate the sum of all elements var numbers = [15.5,2.3,1.1,4.7]; var res = numbers.reduce (function (total, currentValue) {return total + = currentValue}, 0) console.log (res); / / 23.6 / / calculate the sum of elements greater than 4 var result = numbers.filter (item = > item > 4). Reduce ((total, item) = > total + = item, 0) console.log (result); / / 20.26, merge array
Usage: var arr = [... Array 1. Array 2]
Result: concatenate the element value of array 2 to the value of array 1 element
Var arr = [1,2,3] var arr2 = [4,5,6] var res = [... arr,... arr2] console.log (res); / / output result: [1,2,3,4,5,6] var res = [... arr2,... arr] console.log (res) / output result: [4, 5, 6, 1, 2, 3] after reading this article, I believe you have some understanding of "what are the commonly used JavaScript array methods". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.