Split string based on spaces and read that in angular2

Abhinav Mishra picture Abhinav Mishra · Jul 4, 2017 · Viewed 71.1k times · Source

I am creating a pipe in angular2 where I want to split the string on white spaces and later on read it as an array.

let stringToSplit = "abc def ghi";
StringToSplit.split(" ");
console.log(stringToSplit[0]);

When I log this, I always get "a" as output. Where I am going wrong?

Answer

Radu Cojocari picture Radu Cojocari · Jul 4, 2017

Made a few changes:

let stringToSplit = "abc def ghi"; let x = stringToSplit.split(" "); console.log(x[0]);

The split method returns an array. Instead of using its result, you are getting the first element of the original string.