Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECMAScript 5 数组的两个归并方法 #4

Open
ychow opened this issue Mar 10, 2015 · 0 comments
Open

ECMAScript 5 数组的两个归并方法 #4

ychow opened this issue Mar 10, 2015 · 0 comments

Comments

@ychow
Copy link
Owner

ychow commented Mar 10, 2015

ECMAScript 5新增了两个数组的归并方法:reduce() 和 reduceRight() 。 这两个方法都会迭代数组的所有项,然后构建最终的一个返回值。reduce() 方法从数组的第一项开始,逐个遍历到最后;reduceRight() 方法则从最后一项开始,向前遍历到第一项。

var numbers=[1,2,3,5,6,7,8];

var reduceTotal=numbers.reduce(function(prev,cur,index,array){
    return prev+cur;
});

console.log(reduceTotal);      //32

var reduceRightTotal=numbers.reduceRight(function(prev,cur,index,array){
    return prev+cur;
});

console.log(reduceRightTotal);      //32

浏览器支持:IE9+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant