On this podcast we talk about the Array prototype Array.Reduce().  Here are some quick code examples for the using the reduce function:

 

const x = [ 2, 4, 6, 8];
x.reduce( (acc, val) => {
 return acc + val;
}, 0);
// returns 20


const y = [ 'foo', 'bar', 'baz'];
y.reduce( (acc, val, index) => {
  if (index !== 1 ) {
    acc[val] = index;
  }
  return acc;
}, {}) // {foo: 0, baz: 2}


Useful Links:

Array.reduce() MDN

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fullstack Javascript Podcast © 2017