Hannan Rhodes
Hannan Rhodes

Reputation: 89

How can i separate a string from mongoDB

Im taking info from a mongodb server and one of the values is a string like this

Fire, Water, Ice, Steel

Sadly its not an array and will be hard to separate. How can i do this?

Upvotes: 0

Views: 43

Answers (2)

Rob Axelsen
Rob Axelsen

Reputation: 1024

Sadly its not an array and will be hard to separate. How can i do this?

You can make use of the split() method to solve this problem.

Example:

var myString = 'Fire, Water, Ice, Steel';
var myArray = myString.split(', ');
console.log(myArray); // [ 'Fire', 'Water', 'Ice', 'Steel' ]

Upvotes: 1

curv
curv

Reputation: 3844

You can use the javascript split function, this will return you an array of parts.

var str = "Fire, Water, Ice, Steel".split(", ");
console.log(str); // ["Fire", "Water", "Ice", "Steel"];

Upvotes: 3

Related Questions