The new method ES6 array

forEach

forEach()Looks through the array, the return value is not within the loop body, forEach()the loop does not change the contents of the original array, forEach()there are three parameters, the first parameter is the current element, the second parameter is the index of the current element, the third parameter is the current element belongs array.

let array = [1, 2, 3, 4]; array.forEach((item, index, arr) => {   console.log(item); }); 

forEach()Usage is probably the case, but you do not know the hair did not find a problem, forEach()can not be out of the loop, I mean out of the loop is out of the whole cycle, rather than out of the current cycle, said rumor, directly on the code.

let array = [1, 2, 3, 4, 5]; array.forEach((item) => { if (item > 2) { return; } console.log(item); }); 

Ladies and gentlemen, the above code will output Shane?
The answer is: 12
this time someone will say, this is not out of the loop yet, not really, it's still one by one to traverse the array again, we will be the output from another location Found.

let array = [1, 2, 3, 4, 5]; array.forEach((item) => { console.log(item); if (item > 2) { return; } }); 

This time how, the output of 12 345 bar.
After seeing the above two pieces of code, not difficult to find, in forEach()writing return the loop body, but can jump out of the next cycle of the current cycle to execute, and can not jump out of the whole cycle .
Let me say what forEach()cycle does not return a value, this question after reading map next cycle will understand.

map

map()The main role, in fact, is to create a new array map()of parameters and forEach()is the same, not here in say, directly on the examples.

let array = [1, 2, 3, 4 ,5]; let temp = array.map((item, index, arr) => { return item + 1; }); console.log(temp); console.log(array); 

Guess output tempand arrvalues are what
the TEMP: [2, 3, 4, 5, 6]
arr: [1, 2, 3, 4, 5]
Ladies and gentlemen, here should understand map()and forEach()what's the difference, right, use map () loop through the array, you can return a new array, and the array does not change the original content.
of course, map()could be so used, directly to the array elements are converted to strings.

let temp = array.map(String);

filter

Then he says that filter(), filter()parameters and forEach()is the same, filter()mainly filtering, to filter elements in the array does not satisfy the conditions to meet the conditions of the elements into the new array, and will not change the original array.
How to use it, directly on the code.

let array = [1, 2, 3, 4, 5]; let temp = array.filter((item, index, arr) => { return item > 3; }); console.log(temp); console.log(array); 

What will it output, temp4, 5, arraythere is no change, clear, is not it than with the formore convenient cycle hard to write.

every

every()I will not explain to the Senate, and all the same, mainly to talk about every()the role, it will loop through the array, write cycle conditions in vivo, If every item true, it will return true, as long as there is one false, it will return falsethe following look at the example code.

let array = [1, 2, 3, 4, 5]; let bo = array.every((item, index, arr) => { return item > 2; }); console.log(bo); 

The output of this I do not say it, certainly falseah, do not have to explain it.

some

some()And what do na, likewise, each loop through the array, and then to judge according to the condition of the body of the loop, as long as there is one true, will stop the cycle.

let array = [1, 2, 3, 4, 5]; array.some((item, index, arr) => { console.log(item); return item > 3; }); 

According to the output of the item, we can know how many times a total circulation.

reduce

reduce(), Official description: receiving a function as an accumulator, each value in the array (left to right) starts shrinking to a final value of reading these words, a heart inexplicable FML say, 'what this stuff' in fact, plainly, reduce()the function receives is the callback function, the callback function is called an array where every element until the end of the cycle.
reduce()with several other methods are not the same, it has four parameters in order are the last value, index array current value, the current value of the parameter introduction, direct look at an example.
If there is a array, the elements of which are digital, and digital to be calculated now, the situation is normal loop array directly, and then get hold of intermediate variables one by one plus the bar, but spent reduce()on provincial thing more

let array = [1, 2, 3, 4, 5]; let total = array.reduce((a, b) => { return a + b; }); console.log(total); // 15



Guess you like

Origin www.cnblogs.com/yebai/p/11238261.html