Question: Is there an elegant way to chain join() after a foreach()?

Question

Is there an elegant way to chain join() after a foreach()?

Answers 1
Added at 2016-12-13 21:12
Tags
Question

I am trying to write something like this:

DOMElement
 .innerHTML
 .toLowerCase()
 .split(' ')
 .forEach(function(word) {
   return word.charAt(0).toUpperCase() + word.slice(1);
 })
 .join(' ')

Since join needs to receive an array, is there an elegant way to provide it one?

Answers
nr: #1 dodano: 2016-12-13 21:12

You could replace Array#forEach with Array#map.

The map() method creates a new array with the results of calling a provided function on every element in this array.

.map(function(word) {
    return word.charAt(0).toUpperCase() + word.slice(1);
})
Source Show
◀ Wstecz