0

I have a variable with a string attached to it. I'm trying to convert this string into an array with each new line being a new entry.

const song = `Baby Shark doo doo doo doo doo doo
Baby Shark doo doo doo doo doo doo
Baby Shark doo doo doo doo doo doo
Baby Shark!`

I expecting something like this: ["Baby Shark doo doo doo doo doo doo", "Baby Shark doo doo doo doo doo doo", "Baby Shark doo doo doo doo doo doo", "Baby Shark!"]

I'm drawing a blank what would be the cleanest way to solve this?

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Cristof.M
  • 25
  • 7

1 Answers1

2

Split on a newline \n:

const song = `Baby Shark doo doo doo doo doo doo
Baby Shark doo doo doo doo doo doo
Baby Shark doo doo doo doo doo doo
Baby Shark!`;
const arr = song.split("\n");
console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: auto; }
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79