This post shows how to use for..in loopTypeScript with an example.
Read more at https://www.javaguides.net/2019/10/typescript-for-loop-forof-loop-for-in.html
Read more at https://www.javaguides.net/2019/10/typescript-for-loop-forof-loop-for-in.html
for..in loop Examples
The for...in loop iterates through a list or collection and returns an index on each iteration.
// for...in Loop example
let intArray = [10, 20, 30, 40];
for (var index in intArray) {
console.log(index); // prints indexes: 0, 1, 2, 3
console.log(intArray[index]); // prints elements: 10, 20, 30, 40
}
// iterate over object properties example
let user = {
"firstName" : "ramesh",
"lastName" : "fadatare",
"fullName" : "ramesh fadatare"
}
for (const key in user) {
if (user.hasOwnProperty(key)) {
const element = user[key];
console.log(element);
}
}
Output:
C:\typescript-tutorial> tsc for-loops.ts
C:\typescript-tutorial> node for-loops.js
0
10
1
20
2
30
3
40
ramesh
fadatare
ramesh fadatare
Comments
Post a Comment