As in latest release of Angular (v6.1.0) , Angular Team has added new built in pipe for the same named as keyvalue pipe to help you iterate through objects, maps, and arrays, in the common module of angular package.
For example -
<div *ngFor="let item of testObject | keyvalue">
Key: <b>{{item.key}}</b> and Value: <b>{{item.value}}</b>
</div>
To keep original order, use keyvalue:onCompare,
and in component define callback:
// ...
import {KeyValue} from '@angular/common';
@Component(/* ... */)
export class MyComponent {
private onCompare(_left: KeyValue<any, any>, _right: KeyValue<any, any>): number {
return -1;
}
}
Working Forked Example
check it out here for more useful information -
- https://github.com/angular/angular/blob/master/CHANGELOG.md#features-3
- https://github.com/angular/angular/commit/2b49bf7
If you are using Angular v5 or below or you want to achieve using pipe follow this answer
- access key and value of object using ngfor
As in latest release of Angular (v6.1.0) , Angular Team has added new built in pipe for the same named as keyvalue pipe to help you iterate through objects, maps, and arrays, in the common module of angular package.
For example -
<div *ngFor="let item of testObject | keyvalue">
Key: <b>{{item.key}}</b> and Value: <b>{{item.value}}</b>
</div>
To keep original order, use keyvalue:onCompare,
and in component define callback:
// ...
import {KeyValue} from '@angular/common';
@Component(/* ... */)
export class MyComponent {
private onCompare(_left: KeyValue<any, any>, _right: KeyValue<any, any>): number {
return -1;
}
}
Working Forked Example
check it out here for more useful information -
- https://github.com/angular/angular/blob/master/CHANGELOG.md#features-3
- https://github.com/angular/angular/commit/2b49bf7
If you are using Angular v5 or below or you want to achieve using pipe follow this answer
- access key and value of object using ngfor
Have Object.keys accessible in the template and use it in *ngFor.
@Component({
selector: 'app-myview',
template: `<div *ngFor="let key of objectKeys(items)">{{key + ' : ' + items[key]}}</div>`
})
export class MyComponent {
objectKeys = Object.keys;
items = { keyOne: 'value 1', keyTwo: 'value 2', keyThree: 'value 3' };
constructor(){}
}
How to read [object object] using *ngFor in angular2
How to loop over object properties with ngFor in Angular
angular - How to iterate object keys using *ngFor - Stack Overflow
How do you use ngFor to go over nested objects? Stackoverflow answers are just not cutting it!
Videos
In Angular 6.1 the KeyValuePipe was introduced which allows you to iterate object properties:
<div *ngFor="let item of object | keyvalue">
{{item.key}}:{{item.value}}
</div>
Docs: https://angular.io/api/common/KeyValuePipe
I don't know if this is safe, but for these simple cases i don't like the pipe solution, so i usually use:
<div *ngFor="let k of objectKeys(yourObject)">
{{yourObject[k].color}}
</div>
and in the controller:
objectKeys(obj) {
return Object.keys(obj);
}
This is a quite frequent case, i don't understand why there isn't a standard implementation for this like in Angular.js 1.x
Angular 6.0.0
https://github.com/angular/angular/blob/master/CHANGELOG.md#610-2018-07-25
introduced a KeyValuePipe
See also https://angular.io/api/common/KeyValuePipe
@Component({ selector: 'keyvalue-pipe', template: `<span> <p>Object</p> <div *ngFor="let item of object | keyvalue"> {{item.key}}:{{item.value}} </div> <p>Map</p> <div *ngFor="let item of map | keyvalue"> {{item.key}}:{{item.value}} </div> </span>` }) export class KeyValuePipeComponent { object: {[key: number]: string} = {2: 'foo', 1: 'bar'}; map = new Map([[2, 'foo'], [1, 'bar']]); }
original
You can use a pipe
@Pipe({ name: 'keys', pure: false })
export class KeysPipe implements PipeTransform {
transform(value: any, args: any[] = null): any {
return Object.keys(value)//.map(key => value[key]);
}
}
<div *ngFor="let key of objs | keys">
See also How to iterate object keys using *ngFor?
I think the most elegant way to do that is to use the javascript Object.keys like this (I had first implemented a pipe for that but for me, it just complicated my work unnecessary):
in the Component pass Object to template:
Object = Object;
then in the template:
<div *ngFor="let key of Object.keys(objs)">
my key: {{key}}
my object {{objs[key] | json}} <!-- hier I could use ngFor again with Object.keys(objs[key]) -->
</div>
If you have a lot of subobjects you should create a component that will print the object for you. By printing the values and keys as you want and on an subobject calling itselfe recursively.
Hier you can find an stackblitz demo for both methods.
Hi everyone, newbie to angular and I am having some trouble getting all my values in html from an object. The following is stored in results: {}
{
"term": {
"amount": 36,
"label": "Term"
},
"firstPayment": {
"amount": 421.88,
"label": "First Payment"
},
}I would like to then output it in a list like this:
<div *ngFor="NOTHING HERE WORKS">
<ul class="list-group" > <li > <div > <span >{{LABEL VALUE TO GO HERE}}</span> </div> <div> <span>{{AMOUNT VALUE TO GO HERE}}</span> </div> </li> </ul> </div>
since its nested *ngFor="let result of results | keyvalue "> doesn't work, and all the suggestions on stackoverflow also dont work :/
- {{ obj.value[“label”] }} {{ obj.value[“amount”] }}