How to iterate Angular Keyvalue Pipe Properties in order?
Last Updated :
28 Apr, 2025
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 order as given in JSON.
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 will display the keyvalue automatically sort the JSON based upon keys, and will find that data is appearing in a different order than that provided in the JSON object.
HTML
<!-- app.component.html -->
<h2 style="color: green">GeeksforGeeks</h2>
<h2>
How to iterate angular keyvalue
pipe properties in order?
</h2>
<div *ngFor="let item of gfg |keyvalue">
<b>{{item.key}}</b>
{{set(item.value)}}
<div *ngFor="let element of gfg2 |keyvalue ">
<ul>
<li>{{element.value}}</li>
</ul>
</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 =
{
"car":
{
"color": "red",
"model": "2013"
},
"motorcycle":
{
"color": "pink",
"model": "2016"
},
"bicycle":
{
"color": "green",
"model": "2011"
}
}
gfg2: any
set(obj: any) {
this.gfg2 = 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: We can notice that even in JSON object order was 'car,motorcycle,bicycle' , but keyvalue sorted these objects based upon keys like 'bicycle,car,motorcycle'.

Example 2: In this example, we will create a function keepOrder and call it with keyvalue. We have implemented this function in the ts file, by receiving 2 KeyValue pairs and simply returning 0.
HTML
<!-- app.component.html -->
<h2 style="color: green">GeeksforGeeks</h2>
<h2>
How to iterate angular keyvalue
pipe properties in order?
</h2>
<div *ngFor="let item of gfg |keyvalue :keepOrder">
<b>{{item.key}}</b>
{{set(item.value)}}
<div *ngFor="let element of gfg2 |keyvalue :keepOrder ">
<ul>
<li>{{element.value}}</li>
</ul>
</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 =
{
"car":
{
"color": "red",
"model": "2013"
},
"motorcycle":
{
"color": "pink",
"model": "2016"
},
"bicycle":
{
"color": "green",
"model": "2011"
}
}
gfg2: any
set(obj: any) {
this.gfg2 = obj
}
keepOrder =
(x: KeyValue<string, any>, y: KeyValue<string, any>): number => {
return 0
}
}
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:Â We can see that objects appeared the same way as mentioned in the JSON as 'car,motorcycle,bicycle'

Similar Reads
How to iterate over Object in Angular ? Objects consist of a set of key-value pairs, which are known as Properties. All Properties are named in JavaScript objects and the key part represents the Property name, while the value part represents the property Value. Each element(key-value pair) of the object can be utilized to perform a specif
3 min read
How to get comma separated KeyValue Pipe in Angular ? 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
3 min read
How to iterate over the keys and values with ng-repeat in AngularJS ? The task is to iterate over a JS object (its keys and values) using the ng-repeat directive. This can be done using parenthesis in the ng-repeat directive to explicitly ask for a key-value pair parameter from angularJS. Here the variable key contains the key of the object and value contains the valu
3 min read
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 Iterate Over Object Properties in TypeScript In TypeScript, Objects are the fundamental data structures that use key-value pair structures to store the data efficiently. To iterate over them is a common task for manipulating or accessing the stored data. TypeScript is a superset of JavaScript and provides several ways to iterate over object pr
3 min read
How to limit the ngFor Loop to Two Iterations in Angular ? The NgFor is used as a structural directive that renders each element for the given collection each element can be displayed on the page. In this article, we will learn how to Limit the ngFor loop to two iterations. Table of Content Steps for Installing & Configuring the Angular ApplicationProje
3 min read
How to render an Object in a Sorted order based upon Key in Angular ? An Object is a collection of properties, and a property is an association between a name (or key) and a value. A Property's value can be a function, in which case the property is known as a method. To achieve this, we can display the object's properties in a particular order, where the order is dete
3 min read
How to limit ngFor repeat to some number of items in Angular ? In AngularJS, we can use the ngFor directive to loop over a list of items and display them on a webpage. Although, there could be some cases where we don't want to use all the elements in the list but want to display only a limited number of initial items, or the paginated items from the list. In th
3 min read
How to Iterate over Enum Values in TypeScript? Enums in TypeScript are a way to define a set of named constants. Sometimes we may need to iterate over all the values of an enum. There are several approaches to iterate over the enum values in TypeScript which are as follows: Table of Content Using a for...in loopUsing Object.values()Using a forâ¦o
2 min read