Skip to content
Advertisement

Convert 2D Array of words into single Array [closed]

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?

JavaScript

Advertisement

Answer

A simpler logic

  • Convert your multi dimensional array to a single dimensional array with Array.flat Reference
  • Convert this array to a single string using Array.join Reference
  • Spilt this single sting on spaces using regex /s+/ with String.split Reference
  • Find the length of the generated array.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement