Break down elements in array to words with no spaces

Hello,
I have array outputs of words that are elements with 2 words. I.e. (9) [“is”, “is”, “runs over”, “is running”, “should be”, “swam”, “was”, “froze”, “don’t cry”]

what is the best way to turn the array into elements with only one word?
[“is”, “is”, “runs”, “over”, “is”, “running”, “should”, “be”, “swam”, “was”, “froze”, “don’t”, “cry”]

Map/filter/reduce is the technique for how such things are done. In this case we don’t need filter.

var newArr = arr.map(function splitWords(item) {
  return item.split(" ");
}).reduce(function combine(arr, parts) {
  return arr.concat(parts);
}, []);

Which can be done using ES6 techniques, as:

var newArr = arr.map((item) => item.split(" "))
    .reduce((arr, parts) => arr.concat(parts), []);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.