Question: I have an array of arrays with sentences in each element. I want to end up with one single array with each word split up by spaces and then get the length of the whole array

I know I need to loop through the array and then apply split but is there a better way?

```js
const temp2 = [
  ['this is an array'],
  ['this is a scentence'],
  ['I love stackoverflow']
]
console.log(
  temp2[0][0].split(' ').length // 3 words
)  
```

Answer: 
[flatMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap) to the rescue

I use `.split(" ")`, you can use `.split(/ +/)` if there are possibly multiple spaces and `.split(/\s+/)` for all types of whitespace

```js
const temp2 = [
  ['this is an array'],
  ['this is a scentence'],
  ['I love stackoverflow']
]
console.log(
  temp2.flatMap(arr => arr[0].split(" ")).length 
)
```

If you have more than one entry per nested array, you can do

```
  temp2.flatMap(arr => arr.join(" ").split(/ +/))

```

```js
const temp2 = [
  ['this is an array', 'this is a scentence'],
  ['I love stackoverflow']
]
console.log(
  temp2.flatMap(arr => arr.join(" ").split(/ +/)).length
)
```

Last alternative is to count words `.match(/\b\w+\b/g)`

```js
const temp2 = [
  ['this is an array'],
  ['this is a scentence'],
  ['I love stackoverflow']
]
console.log(
  temp2
    .map(arr => arr[0].match(/\b\w+\b/g).length)
    .reduce((a,b)=> a+b)
)
```
You can use [Array#floatMap()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap) and [String#match()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Match):

```js
const temp2 = [['this is an array'], ['this is a scentence'], ['i love stackoverfloe']]

const arr = temp2.flatMap(([s]) => s.match(/\S+/g))
console.log(arr.length)
```

Or [Array#reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) and [String#match()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Match):

```js
const temp2 = [['this is an array'], ['this is a scentence'], ['i love stackoverfloe']]

const total = temp2.reduce((a, [c]) => a + c.match(/\S+/g).length, 0) 
console.log(total)
```