Looping through Arrays in ES5 javascript.
Simple for
let array = [1,2,3];
for (let i =0; i <array.length;i++){
console.log(array[i];
}
for-each:
let array=[1,2,3];
array.forEach(function(value){
console.log(value);
}
for-in:
let array=[1,2,3];
for(let a in array){
console.log(a):
}
for-of:
let array=[1,2,3];
for(var a in array){
console.log(a)
}
ng-repeat vs *ngFor
ng-repeat available in the angular1.X
and *ngFor is available in the angular 2 or more
Simple for
let array = [1,2,3];
for (let i =0; i <array.length;i++){
console.log(array[i];
}
for-each:
let array=[1,2,3];
array.forEach(function(value){
console.log(value);
}
for-in:
let array=[1,2,3];
for(let a in array){
console.log(a):
}
for-of:
let array=[1,2,3];
for(var a in array){
console.log(a)
}
ng-repeat vs *ngFor
ng-repeat available in the angular1.X
and *ngFor is available in the angular 2 or more
- ng-repeat directive instantiated template once per item for a collection.
- Each template instance had its own scope.
- Special properties were available for each template instance: $index, $first, $middle, $last, $even, $odd.
- ng-repeat by default did not allow duplicate elements. A tracking function was responsible for this task.
- In order to add duplicate items, track by expression was used.
$scope.items=[1,2,3,4,5,6]
<div ng-repeat="item in items">{{item}}</div>
or
<div ng-repeat="item in items track by $index">{{$index+1}}:{{item}}</div>
here index start with the 0
In the angular 2 or more
<ul>
<li> *ngFor="#item of items">{{item}}</li>
</ul>
Tags:
Angular