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, angular 10, angular 11 and angular 12 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...