JavaScript Merge method reduce () and reduceright ()

Source: Internet
Author: User
Tags prev

ECMAScript 5 also added two methods for merging arrays: reduce () and reduceright (). Both of these methods iterate over all the items of an algebraic group and then build a value that is ultimately returned. where the reduce () method starts with the first item in the array and traverses through to the end. The Reduceright () begins with the last item in the array and traverses forward to the first item. The reduce () and reduceright () functions receive 4 parameters: the previous value is the index array object of the current value entry. This function returns any value that is automatically passed to the next item as the first argument. The first iteration occurs on the second item of the array, so the first parameter is the first item of the array, and the second is the second item of the array.
 1  var  values = [1,2,3,4,5]; 2  var  sum = values.reduceright (function   (Prev,cur,index,array) { 3  return  prev+cur;  4  });  5  // 15  
Code Analysis: First execute callback function, Prev=1, cur=2.                 Second time prev=3, cur=3. This process continues until each item in the array is accessed again, and the result is returned. About the compatibility of reduce () and Reduceright (): ie9+, FF, Safari, Opear 10.5 and Chrome.

JavaScript Merge method reduce () and reduceright ()

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.