Typescript Convert Object to Array - because *ngFor does not supports iteration of object Typescript Convert Object to Array - because *ngFor does not supports iteration of object typescript typescript

Typescript Convert Object to Array - because *ngFor does not supports iteration of object


You can use Object.keys(obj) to get named indexes. This will return an array structure which you can use/customize further. A sample use to iterate over object values may look like this

var persons = {     john: { age: 23, year:2010},    jack: { age: 22, year:2011},    jenny: { age: 21, year:2012}}

Getting an iterator

var resultArray = Object.keys(persons).map(function(personNamedIndex){    let person = persons[personNamedIndex];    // do something with person    return person;});// you have resultArray having iterated objects 


Since Angular 6, there is now a keyvalue pipe operator. Simple do the following:

*ngFor="let item of objectList | keyvalue"item.key # refers to the keys (134, 135...) in your exampleitem.value # refers to the object for each key


If you don't need the indexes, you can use Object.values(yourObject) to get an array of the inner objects.