
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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
- Related Articles
- How to make Ulam number sequence in JavaScript?
- Finding closed loops in a number - JavaScript
- How to get almost increasing sequence of integers in JavaScript ?
- Reverse array with for loops JavaScript
- Demonstrate nested loops with return statements in JavaScript?
- For Loops in Javascript
- How to get the number of forms in a document with JavaScript?
- How to get a decimal portion of a number with JavaScript?
- How to use else statement with Loops in Python?
- How to get the number of options in the dropdown list with JavaScript?
- How to use single statement suite with Loops in Python?
- MySQL query to get the next number in sequence for AUTO_INCREMENT field?
- How to get the cosine of a number in JavaScript?
- How to get the sine of a number in JavaScript?
- How to get the tangent of a number in JavaScript?

Advertisements