How to make an array from a string by newline in JavaScript? How to make an array from a string by newline in JavaScript? javascript javascript

How to make an array from a string by newline in JavaScript?


Use split()

Fore example

str = "abc\ndef";console.log(str.split("\n"));

will print out

["abc", "def"] 


Use JavaScript .split() function to create an array with elements split by '\n'and then manually iterate through that array and add '<' for each item. The following code may help :

var str="How\nare\nyou\ndoing\ntoday?";var n = str.split("\n");for(var x in n){       n[x]= '>'+n[x];     alert(n[x]);}