Sometime we need to push objects into array in Angular or to iterate the objects. Today I'll explain how we can do it in an easy proficient way.
Here I have declared that allitems is an array of any type. And we got some users data from the web service is assigned to the allitems array.
1 2 |
public allItems: any[]; this.allItems = data.result.users; |
Here allItems is an array of object and I will iterate this array like:
1 2 3 4 5 6 7 8 9 10 |
// declaring array let exportData:any[] = []; enum Gender { Other = 3, Male = 1, Female = 2 }; this.allItems.forEach(item => { // data manipulation part let gender = Gender[item.gender] || Gender.Other; // here we are pushing objects into exportData array. exportData.push({'firstName': item.firstName,'lastName': item.lastName ,'dob': item.dob,'gender': gender}); }); |