Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get sequence number in loops with JavaScript?
To get sequence number in loops, use the forEach() loop. Following is the code −
Example
let studentDetails =
[
{
id: 101, details: [{name: 'John'}, {name: 'David'},{name: 'Bob'}]},
{id:102, details: [{name:'Carol'},{name:'David'},
{name:'Mike'}]
}
];
var counter = 1;
studentDetails.forEach(function(k){
k.details.forEach(function(f) {
console.log(counter++);
}
);
});
To run the above program, you need to use the following command −
node fileName.js.
Output
Here, my file name is demo159.js. This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo159.js 1 2 3 4 5 6
Advertisements