In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the operation method of ECMAScript5 array in WeChat Mini Programs". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought slowly and deeply, and study and learn "what is the operation method of ECMAScript5 array in WeChat Mini Programs".
First of all, it is added that ECMAScript is a scripting language specification developed by the European Association of computer Manufacturers (European Computer Manufacturers Association) on the basis of JavaScript1.1 in order to unify all kinds of script. ECMAScript5 is the fifth version of this specification. Because it has received a positive response from major browser manufacturers, it can be called the future of JavaScript. WeChat Mini Programs also supports ECMAScript5.
Array method in ECMASript5
ForEach
The forEach method provides a relatively simple (at least formally) way to traverse an array, which has three parameters: the array element, the index of the element, and the array itself.
Data = [1,2,3,4,5]
/ / Sum of all elements
Var sum = 0
Data.forEach (funtion (value) {sum + = value;})
Sum / / = > 15
/ / each element multiplied by 2
Data.forEach (function (v, I, a) {a [I] = v * 2;})
Data / / = > [2,4,6,8,10]
Do you understand? It doesn't matter if you don't understand. Let's take multiplying 2 as an example to explain it a little bit. The code may not execute, just for illustration purposes.
The implementation of forEach is roughly as follows, as you might imagine, the deviation should be small.
Function forEach (f)
{
For (int I = 0; I
< array.length; i++) f(array[i], i, array); } 用户定义一个函数,参数为数组元素,元素的索引和数组本身。之所以这样定义是forEach方法实现的要求。 function mul2(v, i, a) { a[i] = v * 2; } 接下来是调用 data.forEach(mul2); 这样就好理解多了。示例中只是将定义乘2函数和调用该函数合并成了一行。 map 这个方法和映射没有任何关系,功能是将数组中的每个元素传递给指定的函数进行处理,并将返回值合成一个数组返回。 a = [1, 2, 3]; b = a.map(function(value){ return value * 2}); //=>B = [2, 4, 6]
Flter
Filters each element in the array and returns an array of eligible elements.
A = [1,2,3,4,5,6,7]
B = a.filter (function (x) {return x% 2percent = 0}); / / = > b = [2,4,6]
Every and some
The every method and the some method determine the logic of the array.
A = [1, 2, 3, 4, 5]
A.every (fuction (x) {return x > 3}); / / = > false.
This method calls the specified function on all elements of the array, and the result of the every method is true only if all (every) results are true, otherwise the result is false.
A = [1, 2, 3, 4, 5]
A.some (fuction (x) {return x > 3}); / / = > true.
This method calls the specified function on all elements of the array. As long as some (some) result is true, the result of the some method is true, and the result is false only if all the results are false.
Reduce and reduceRight
The reduce and reduceRight methods combine array elements using the specified function to produce a single value.
A = [1, 2, 3, 4, 5]
Factorial = a.reduce (function (prev, v) {return prev * v})
This is an example of calculating factorial. The two parameters of the specified function are the result of the previous calculation and the new element value. The reduce method calls the specified function on all elements. In fact, in addition to the function, reduce has another parameter that specifies the initial value of the calculation. If not specified, the first element of the array is used as the initial value.
The function of reduceRight is similar to that of reduce, except that the direction of calculation is from back to front.
IndexOf and lastIndexOf
The indexOf method searches for elements in the array, finds the index value of the first element with the given value, and returns-1 if it is not found. You can use the optional second parameter to specify the starting position of the retrieval.
The function of lastIndexOf is similar to that of indexOf, except that the direction of calculation is from back to front.
Thank you for your reading, the above is the content of "what is the operation method of ECMAScript5 array in WeChat Mini Programs". After the study of this article, I believe you have a deeper understanding of what the operation method of ECMAScript5 array in WeChat Mini Programs is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.