Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

array reduce javascript

const numbers = [260, 25, 35];

console.log(numbers.reduce(myFunc))

function myFunc(total, num) {
  return total - num;
}
/*console.log will show 200 since the first element minus 35 and minus 25 is 260-25-35=200*/
/*while the name is "reduce", you do NOT have to subtract. Reduce() is more about "take the first element as total, and do something(whatever the function says) to it for each of the remaining elements; the remaining elements will be identified as the second parameter num for the function myFunc, you do whatever once for all remaining elements*/
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #array #reduce #javascript
ADD COMMENT
Topic
Name
8+3 =