0

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>
1
  • 1
    Can you please let us know what output you want? Commented Jun 18, 2020 at 7:04

1 Answer 1

6

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>
Sign up to request clarification or add additional context in comments.

1 Comment

This is Interesting. i will look into it. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.