How to display key name from object and key value Plz suggest

I dont know how to display key value and I went through the below link also but not able to do exact as given in link

The solution is actualy much simpler -

If you want to iterate key=>value pairs in ngFor (if i correctly understood your question), just set in your component the keys array along with the main object, and iterate keys only like this:

     this.mainObject  =  { 'name': 'Mike', 'age':36, 'job':'programmer'}
     this.keys = Object.keys(this.mainObject);


<ion-item *ngFor="let key of keys">
     <ion-label>{{key}} - {{mainObject[key]}}</ion-label>

Hi thanks its working but my data is like below

 this.mainObject  = [{"id":"1","description":"id is 1"},{"id":"2","description":"id is 2"},{"id":"3","description":"id is 3"}]

when I will do

 this.keys = Object.keys(this.mainObject);

in keys array I will get 0,1 ,2
and above there is same key so may display wrong info

This is given by the structure of your data - in your case you are actualy iterating array of objects, so it’s not really key=>pair issue, you don’t need the Object.keys()

for your data:

<ion-item *ngFor="let item of mainObject">
     <ion-label>ID: {{}} => VALUE: {{item.description}}</ion-label>

yes the same way I am using but the below object

 this.mainObject  = [{"id":"1","description":"id is 1"},{"id":"2","description":"id is 2"},{"id":"3","description":"id is 3"}]

can be replaced by like below

 this.mainObject  = [{"key":"5","description":"key is 5"}]

Object.keys is ok but I want to use this in ionic html then it may work

Not sure if i understand correctly but -
You can wrap Object.keys in a function, and use nested ngFor like this:

     this.mainObject  =  [{ 'name': 'Mike', 'age':36, 'job':'programmer'},{ 'name': 'John', 'age':35, 'job':'cook'}]


    return Object.keys(obj);

and in template:

<div *ngFor="let item of mainObject">
    <ion-item *ngFor="let key of keys(item)">
         <ion-label>key: {{key}} => VALUE: {{item[key]}}</ion-label>
1 Like

hey thanks for your help I have solved above problem…,
my html

 <ion-item *ngFor="let item of items">      
         <span *ngIf="getKeys(item)">
          <div *ngFor="let key of keys">
              {{key}} :  {{item[key]}}              

in ts file

 public getKeys(data){
    return true;
1 Like

hey mine is same like yours and yours is well structured in html …
but getting this below error

Cannot find a differ supporting object 'true' of type 'boolean'. NgFor only supports binding to Iterables such as Arrays.

if you are trying to use my code with your getKeys function, make sure it returns the keys, not “true” like in your example :wink:

okkk thanks atleast better than pipe :slight_smile:

This is a terrible idea. You should never do anything in a function called by ngIf that modifies state.

1 Like

hmm thanks rapropos :+1:

hello , can someone help me to extract detail of any selected competence in another component called detail

*ngFor=“let key of keys; let i = index”
[index]= “i”


<a style=“cursor:pointer;” [routerLink]="[index]" routerLinkActive=“active” class=“list-group-item clearfix”>



***** wht can I use in detail component ?? ***

ngOnInit() {

console.log(this.competence); // undefined !!


Look at the conference app schedule and session detail pages.

1 Like

thank you very much :slight_smile:

Sorry I am a beginner in this field, it is the first time I develop with angular, can you help me please , in fact i don’t know if i am in the true forum or not , my problem is that I will dispose detail of an object of object getting from API , it’s not a table that’s why i have a problem , thank you for help

Thanks man. working fine to me