How to get comma separated KeyValue Pipe in Angular ?
Last Updated :
28 Apr, 2025
The KeyValue Pipe is an Angular built-in feature that transforms objects or maps into an array of key-value pairs. We can use the last variable of *ngFor directive to achieve the desired result. We will compare that if the element is last then add a comma. In this article, we will learn how to get a comma-separated KeyValue Pipe in Angular.
Steps for Installing & Configuring the Angular Application
Step 1: Create an Angular application using the following command.
ng new appname
Step 2: After creating your project folder i.e. appname, move to it using the following command.
cd appname
Project Structure
It will look like the following:
Example 1: In this example, we use the last variable of *ngFor directive to achieve the desired result. We will compare that if the element is last then add a comma.
HTML
<!-- app.component.html -->
<h2 style="color: green">
GeeksforGeeks
</h2>
<h2>
How to get comma separated
KeyValue Pipe in Angular?
</h2>
<div *ngFor="let item of gfg | keyvalue; last as isLast">
<b>{{item.key}}:</b> {{item.value}}
<span *ngIf="!isLast">, </span>
</div>
JavaScript
// app.component.ts
import { Component, OnInit }
from '@angular/core';
import { KeyValue }
from '@angular/common';
import { Pipe, PipeTransform }
from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: "./app.component.html",
styleUrls: ['./app.component.css']
})
export class AppComponent {
gfg: any = {
"HTML": "Hyper Text Markup Language",
"CSS": "Cascade Style Sheet",
"XML": "Xtensive Markup Language",
"JS": "Javascript"
}
}
JavaScript
// app.module.ts
import { NgModule }
from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { HttpClientModule }
from '@angular/common/http';
import { AppComponent }
from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Output:

Example 2: In this example, we will comma separate the array of JSON objects using keyvalue. The keyvalue will help to get the keys and values and then we will add a comma
HTML
<!-- app.component.html -->
<h2 style="color: green">
GeeksforGeeks
</h2>
<h2>
How to get comma separated
KeyValue Pipe in Angular?
</h2>
<div *ngFor="let item of gfg | keyvalue ; last as isLast">
<div *ngFor="let element of display(item.value)|keyvalue">
<b>Value: </b>{{element.value}}
<span *ngIf="!isLast">, </span>
</div>
</div>
JavaScript
// app.component.ts
import { Component, OnInit }
from '@angular/core';
import { KeyValue }
from '@angular/common';
import { Pipe, PipeTransform }
from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: "./app.component.html",
styleUrls: ['./app.component.css']
})
export class AppComponent {
gfg: any =
[
{ "3": "1700" },
{ "4": "1900" },
{ "2": "1400" },
{ "1": "1300" },
{ "7": "1200" },
{ "9": "1800" },
{ "8": "2100" },
{ "5": "1100" }
]
display(obj: any) {
return Object.values(obj)
}
}
JavaScript
// app.module.ts
import { NgModule }
from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { HttpClientModule }
from '@angular/common/http';
import { AppComponent }
from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Output:

Similar Reads
How to disable the Default KeyValue Pipe sort in Angular ? The KeyValue Pipe converts a given Object or Map into an array of key-value pairs. For this, we can disable the sorting in keyvalue pipe sort by passing 0 with it. This article will cover disabling the default keyvalue pipe sort in Angular, along with a basic understanding of their implementation wi
3 min read
How to use comma as list separator in AngularJS ? In this article, we will use commas as a list separator in AngularJS applications.In AngularJS, we can use a list separator by simply placing the command between the items in the list. If we have an array of items that we need to display in the application, then we can use ng-repeat to iterate throu
4 min read
How to iterate Angular Keyvalue Pipe Properties in order? The KeyValue Pipe converts a given Object or Map into an array of key-value pairs. We can use this with the ngFor to loop through the object keys. In this article, we will learn How to iterate angular keyvalue pipe properties in order. Here, the order of sequence will be followed exactly in the same
3 min read
How to get the value of the form in Angular ? In this article, we are going to see how to get value from the form in Angular 10. We will use AbstractControl value property to get the form value in an object. Syntax: form.value Return Value: object: It is an object containing form value Approach:Â Create the Angular app to be usedIn app.componen
1 min read
How to Get All Route Params/Data in Angular? In Angular, managing route parameters and extracting data from routes is important for creating dynamic and responsive web applications. Route parameters allow you to pass information in URLs, and accessing this data enables your application to make decisions and render content dynamically. This art
4 min read