Chieko
Chieko

Reputation: 11

Seperate 1 Textarea line as array to store them in MongoDB

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...

Upvotes: 0

Views: 214

Answers (1)

Luke Carr
Luke Carr

Reputation: 571

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.

Upvotes: 1

Related Questions