DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. KeyValue Pipe in Angular 6.1

KeyValue Pipe in Angular 6.1

With the upcoming release of Angular 6.1, a developer takes a look at one of the features included, KeyValue Pipes. Read on to learn more!

Neel Bhatt user avatar by
Neel Bhatt
CORE ·
Aug. 08, 18 · Tutorial
Like (4)
Save
Tweet
Share
18.02K Views

Join the DZone community and get the full member experience.

Join For Free

Note - You can find the source code of my sample application here. You can find all .NET Core posts here.

Angular 6.1 was recently announced, and it comes with some interesting features.

One of those features is, the KeyValue pipe.

Currently, there is not any straight forward way to iterate object keys using ngFor in Angular.

I have seen many StackOverflow questions for this and many workarounds to iterate through objects and arrays in Angular. But now there is a straight and easier way for this.

This can be achieved using the keyvalue pipe.

What can be achieved using keyvalue pipe?

We can create a pipe to an object Map or object (dictionary) to a key value pair array for use in an ngFor.

Example

Let us see how it works.

Let's add a component with objects and array as below:

export class Angular6_1Component {

myValues = { “key1“: “value1“, “key2“: “value2“ };
myObject: {[key: number]: string} = {2: ‘I am two’, 1: ‘I am one’};

myArray = [“I am an array 1”, “I am an array 2”]
myMap = new Map([[2, ‘I am two’], [1, ‘I am one’]]);
}

As you can see above, I have added different objects, an array and a Map, to test the keyvalue pipe.

The next step is to add in the HTML in which we will use keyvalue pipe:

The ode can be copied from my GitHub repo.

This will result in:

As you can see above, the syntax makes it quite easy to iterate objects, array, maps, etc. using *ngFor.

Note that:

  • As Arrays only have values, keys will start from 0.
  • objects\arrays will arrange the order of the keys.
  • If the keys are string, keys will be arranged in alphabetical order.

If you want to see how it works internally, have a look at this commit here.

Hope this helps!

Note - You can find the source code of my sample application here. You can find all .NET Core posts here.

AngularJS .NET

Published at DZone with permission of Neel Bhatt, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • Core Machine Learning Metrics
  • Apache Kafka vs. Memphis.dev
  • Tech Layoffs [Comic]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: