Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
ngFor and For loop over object or Array in angular

ngFor and For loop over object or Array in angular

Aman Jain, March 30, 2022October 4, 2022

Loops are used to iterate through objects or array, In angular we can use ngFor and For loop over array or object in angular, ngFor is a template structural directive which is used to iterate a loop in template file whereas for and each is used to iterate the loop in component. In angular or javascript we can use for loop in 3 types of for loop in javascript or angular

  • for – loops through a block of code a number of times
  • for/in – loops through the properties of an object
  • for/of loops through the values of an iterable object

and in angular template we can use ngFor structural directive through the Array only, to iterate the object in angular template we need to use pipe keyvalue.

Let’s take examples of for loops

ngFor loop iterate over Array or Object

In this example we will use ngFor structural directive in template html file as below

<div *ngFor="let item of [1,2,3,4]"> 
  {{item}}
</div>

ngFor Loop iterate over Array key value

<div *ngFor="let item of [{id:1},{id:2}]"> 
  {{item.id}}
</div>

ngFor Loop iterate over Array with index

<div *ngFor="let item of [{id:1},{id:2}]; let counter=index">
{{item.id}} {{counter}}
</div>

For loop over array or object using for-in, for-of and for

So in this example we will use loop with array, objects and block of code a number of times. As we have explained above loop can be used in three ways so here are examples

 For loops through a block of code a number of times

for(let i=0 ;i<=5 ; i++){
 console.log("value of i ",i);
}

 For loops through a block of code a number of times

for(let i=0 ;i<=5 ; i++){
 console.log("value of i ",i);
}

Output of above:

value of i 0
value of i 1
value of i 2
value of i 3
value of i 4
value of i 5

For-in loop on object

let items:any={id:1, name : 'test', }
for(let item in items){
 console.log("value of item ",item,items[item]);
}

Output of above:

value of item  id 1
value of item  name test

For-of loop on object

let items:any={id:1, name : 'test', }
for(let item in items){
 console.log("value of item ",item,items[item]);
}

Output of above:

value of item  id 1
value of item  name test

For-of loop on Array key value

let items:any=[{id:1, name : 'test'},{id:2, name : 'test2'}]
for(let item in items){
 console.log("value of item ",item.id ,item.name);
}

Output of above:

value of item 1 test
value of item 2 test2

Related

Javascript Angular angularforJavascriptloop

Post navigation

Previous post
Next post

Related Posts

Angular How to Use Interface in Angular with Example

How to Use Interface in Angular with Example?

August 6, 2022March 16, 2024

Interface in angular or in any programming languages is pattern which bind component or class to use same implementation across the modules. In this article we will cover the use of interface in angular with example. In Angular interface can be used on service, factory, component or any type of…

Read More
Angular angular file upload

How to upload files with form data in angular 12?

June 20, 2021November 8, 2023

in this tutorial we will learn to upload files with form data in angular. it’s not easy to upload the files along with form data in angular in compare to JQuery and other JavaScript framework. Angular upload a form in JSON and some time it get difficult to obtain the…

Read More
Javascript How to check user agent in angular

How to check user agent in angular ?

October 18, 2022March 16, 2024

In this post, we will show you how to check user agent in Angular. user-agent header is a string that contains information about the browser and operating system. It is set by the browser and sent to the server with every request. It can also be used to determine if…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version