Najam Us Saqib
Najam Us Saqib

Reputation: 3402

how to iterate *ngFor through Different objects in array in Angular?

I have dynamically generated inconsistent array of Objects from API(server).

E.g:

array = [
 {name: 'blah', age:2},
 {status: 'pending', date: '20-20-2020'},
 {blah: 'foo', google: 'bar'},
 {apple: 'android', microsoft: 'eeewww'}
]

this array could be anything with different keys and values.

how can i Iterate through it to show the values in ngFor?

What I am currently is doing:

this.printArray = JSON.stringify(JSON.parse(this.array), null, 4);

<div *ngFor="let print of printArray">
  {{print}}  // it prints the array like {"name": blah, "age": 2} but i don't want it in this way
</div>

Upvotes: 0

Views: 188

Answers (1)

Eliseo
Eliseo

Reputation: 58029

You can use Angular keyvalue pipe

<div *ngFor="let element of myArray"> 
    <!--element is an object-->
    <div *ngFor="let item of element|keyvalue">
        {{item.key}}:{{item.value}}
    </div>
</div>

Upvotes: 6

Related Questions