I require to split a string into parts, each of 10 words. For example, the string:
let cadena = "atención la descripción de los síntomas, revisó detenidamente las zonas afectadas y explicó detalladamente el problema y el uso de los medicamentos que recetó".
After dividing it would look like:
[
[atención la descripción de los síntomas, revisó detenidamente las zonas]
[afectadas y explicó detalladamente el problema y el uso de los]
[medicamentos que recetó]
]
For this I have used the function split
. This returns an array of words, but I still can't figure out how to join them by groups of 10 words
palabras=cadena.split('')
The easiest solution I could come up with is to simply separate the phrase into tokens (spaces in this case) with the split method from there it's just joining them into groups of 10.
To do this, we use
splice(0, 10)
, which modifies the original array and returns a subarray, which we will later join with the methodjoin(" ")
, which will join all the elements of that array with the specified token or by default a comma.As a note, note that the above will repeat until the space-split array contains no more elements (
fragments.length > 0
).I leave you the snippet of the code that I mention.