Today to do the second question, RT:
Computes the collection of arrays in which the known quantity group is a number type. Sum
A very simple topic, usually after the traversal of each i + = and then return to go out, there is nothing to say, first introduce a few JS method, will not think so.
1.array.prototype.reduce ():
2.forEach, before have written, not clear can see the first article
3.map, ibid.
4. Common for loop
5.eval This is a black technology, feel the back JS design, but very simple, also can meet the needs
1.array.prototype.reduce (): See how compatible, IE9 above, other fully compatible (Chrom,firefox), MDN on the exact words: The reduce()
method applies a function against an Accumulator and every element in the array (from left to right) to reduce it to a single value. Translate the reduce()
method to the accumulator and each of the elements in the array ( From left to right) apply a function that reduces it to a single value.
I also do not understand, look at the grammar:
Array.reduce (function (accumulator currentValue
, Currentindex, array), InitialValue)
Personally, it is that reduce accepts a function with 4 parameters to fill, 1. The initial value, or the return value of the last callback, 2. The element that the array is processing, that is, the i;3 of each traversal in the loop. The element index is equivalent to the loop summary of I;
4. Is the array, need to handle the array, 5.initialValue, is to set the initial value, if not filled, then the first in the array is the initial value, an empty array directly error!!
Idea: Reduce accepts 4 parameters, then we can complete as long as the first two parameters, the return value of the last callback, personal understanding, when the array [1,2,3,4] The first time a parameter is 1, then the second time is the value after the operation, the third is the second value + 4th value, and so on
The index value does not need pass, the array can choose optional, said is not very clear, this picture should see clearly.
The final result is 10,https://developer.mozilla.org/zh-cn/docs/web/javascript/reference/global_objects/array/reduce The original address;
On the code to solve:
var arr = [1,2,3,4,5]; if (Array.prototype.reduce ()) { arr.reduce(A,b) {return A + b},0) }
Returns: 15
If this initial 0 is set to 10, then 10 +15 = 25 returns 25;
2.forEach Problem Solving:
var arr = [1,2,3,4,5]; var num = 0; Arr.foreach (function(val, index, arr) { + = val}) return num// returns to
3.map Problem Solving:
var arr = [1,2,3,4,5]; var num = 0; Arr.map (function(a) { + =A})return num; // back to
4. General circulation
var arr = [1,2,3,4,5]; var num = 0; for (var i = 0;i<arr.length;i++) { + = arr[i]}return num; // back to
5.eval
It's important to avoid using eval in an unnecessary situation!
Eval is special, and he can perform arithmetic on the string of a number class. It belongs to the string class, but he can +-*%,number class, he only accepts string!.
On the code:
var arr = [1,2,3,4,5]; var num = eval (arr.join (' + ')), Console.log (num) console.log (typeof num)// returns the number --. -
Summary: Although Eval is very simple, but still do not use a good point, that is, when the interview hooves hooves, no use, solve the actual map best.
Writing this article is also learning, reduce the API, review the Foreach API, these relatively simple topics, you can practice more, lay a good foundation than anything is important, this is said to himself.
An array of JS (ii)