0

so i want to use multible Data, 1 line as a array string seperate from the Textarea in the Backend from my NodeJS Web Application to save them as a new Data in the Collection:

{"_id":"someid","Name":"Tim"},{"_id":"someid","Name":"Steve"}

This in a Textarea:

Tim
Steve
John

so now in the Backend shuld the Data store in a array to create new MongoDB Data:

var data = ['Tim', 'Steve', 'John'];

I dont know how to seperate the Datas to a Array in the Backend...

Chieko
  • 11
  • 3

1 Answers1

1

If you have a string containing the textarea's contents, you can call string.split("\n") to split it into an array (using the newline character as a delimiter).

So if you had a variable called textarea which contained the textarea's value, it would look something like this:

var textarea = "..."; // Get this from somewhere
var data = textarea.split("\n");

If you want to provide cross-platform support, you should also handle Windows-style line breaks (\r\n) by replacing them with POSIX line breaks before calling .split("\n"), like so: string.replace(/\r\n/g, "\n").split("\n"). See this answer for further explanation surrounding Windows/POSIX line breaks.

Luke Carr
  • 553
  • 5
  • 18