How to Convert Comma Separated String to Array in Angular?

By Hardik Savani | July 14, 2020 | Category : Angular


Here, i will show you how to works how to convert array into comma separated string in angular. you will learn convert string into array angular. if you want to see example of convert comma separated string to array angular then you are a right place. In this article, we will implement a convert comma separated string to json array angular.

I will give you very simple example of convert comma separated string to array angular application. you can easily convert string to array in angular 6, angular 7, angular 8, angular 9 and angular 10 project.

we will use split() function to converting string to array in angular app. split() provide to covert comma separated string to array. so let's see bellow example that will help you easily understand.

Syntax:

split(separator, limit)

you can see example with output:

src/app/app.component.ts

import { Component } from '@angular/core';

@Component({

selector: 'my-app',

templateUrl: './app.component.html',

styleUrls: [ './app.component.css' ]

})

export class AppComponent {

name = 'Angular';

myString = 'Hardik,Paresh,Vimal,Harshad,Kiran';

myArray = this.myString.split(',');

constructor() {

console.log(this.myArray);

}

}

Output:

["Hardik", "Paresh", "Vimal", "Harshad", "Kiran"]

0: "Hardik"

1: "Paresh"

2: "Vimal"

3: "Harshad"

4: "Kiran"

I hope it can help you...




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: