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?

1
  • Use the split() function on string, splitting on newline characters '\n' Commented Apr 24, 2019 at 23:58

1 Answer 1

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; }

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.