string.reduce code example

Example 1: reduce an array of objects to string

var authors = [{
  name: "a"
}, {
  name: "b"
}, {
  name: "c"
}];
var result = authors.reduce(function(author, val, index) {
  var comma = author.length ? ", " : "";
  return author + comma + val.name;
}, '');
console.log(result);

Example 2: .reduce mdn

arr.reduce(callback( accumulator, currentValue[, index[, array]] )[, initialValue])

Example 3: array reduce

arr.reduce(callback( accumulator, currentValue[, index[, array]] ) {
  // return result from executing something for accumulator or currentValue
}[, initialValue]);