简体   繁体   中英

Reduce Method Javascript

Am getting false output for the below code. I know reduce concept in javascript. But the below I could not understand. Please help me with this.

 var numbers = [175, 50, 25]; var res = numbers.reduce(myFunc, []); function myFunc(total, num) { return total.length > num.length; } console.log(res);

Reduce function will not return the array to you it will reduce your array to a sigle value according to the operation perfom inside the function,

so in your example it will always return the false because there is blank array in param and you are comparing that array with total which is single number coming from numbers I think

Please go through the below link which will help you to understand reduce() function in a batter way,

https://www.w3schools.com/jsref/jsref_reduce.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM