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

How to generate component in angular cli?

August 29, 2021August 29, 2021

Angular cli has rich features of generating components, providers, modules and many more. Angular cli can also be used for build production ready product and serve the project for local environment. Usage of Angular cli ng generate command To generate the component ng generate component component_name To generate the modules…

Read More
Javascript Angular KeyUp Event on Input Example

Angular KeyUp Event on Input Example

September 22, 2022March 16, 2024

Angular KeyUp Event on Input is an Angular event binding it triggers when user interacts with DOM like text based input element and when we key up the input. Input events in angular can be handled using input and output decorators and in this tutorial we will learn Angular KeyUp…

Read More
Javascript Laravel Dynamic Autocomplete Using Typehead JS

Laravel Dynamic Autocomplete Using Typehead JS

July 16, 2022July 16, 2022

In this article we will learn to use Laravel Dynamic Autocomplete Using Typehead JS. Typehead JS is useful when we want live search of bulk data. Autocomplete search is mostly work of javascript and when we want to fetch live data from database then we require the intervention of laravel…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version