Mikyas TADESSE
Mikyas TADESSE

Reputation: 159

How to change a phrase string into an array of word string in angularjs

I would like to change a phrase string into an array of word string in angularjs. For example i would like to transform $scope.phrase='come back home' to $scope.words=['come','back','home']

Can you please help

Upvotes: 0

Views: 789

Answers (2)

leo.fcx
leo.fcx

Reputation: 6467

Use split string method:

$scope.words = $scope.phrase.split(' '); // Using space as param

Upvotes: 1

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48367

You need to use split method.

Split method splits a String object into an array of strings.

$scope.words=$scope.phrase.split(' ');

Here is a working solution.

Upvotes: 1

Related Questions