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
  • 40,575
  • 10
  • 44
  • 74
Cristof.M
  • 25
  • 6

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
  • 40,575
  • 10
  • 44
  • 74