YogeshChauhan . com

5 Ways to Loop Through JavaScript Arrays

in JavaScript on December 14, 2020

Similar post:

Quick Introduction To 11 Array Iteration Methods In JavaScript

1. For Loop

The safest way to loop through an array, is using a for loop:


var languages = [
    {id: 1, name: 'PHP', ratings: 8},
    {id: 2, name: 'Python', ratings: 9},
    {id: 3, name: 'C', ratings: 8},
    {id: 4, name: 'Go', ratings: 8},
    {id: 5, name: 'Swift', ratings: 7}
];

for(let i = 0; i < languages.length; i++){
    console.log(languages[i].name);
}

// output
PHP
Python
C
Go
Swift

2. for/of Loop


var languages = [
    {id: 1, name: 'PHP', ratings: 8},
    {id: 2, name: 'Python', ratings: 9},
    {id: 3, name: 'C', ratings: 8},
    {id: 4, name: 'Go', ratings: 8},
    {id: 5, name: 'Swift', ratings: 7}
];

for (let lang of languages) {
    console.log(lang.name)
}

// output
PHP
Python
C
Go
Swift

3. forEach()


var languages = [
    {id: 1, name: 'PHP', ratings: 8},
    {id: 2, name: 'Python', ratings: 9},
    {id: 3, name: 'C', ratings: 8},
    {id: 4, name: 'Go', ratings: 8},
    {id: 5, name: 'Swift', ratings: 7}
];

languages.forEach(function(lang, i) {
    console.log(`Id: ${i}, Name: ${lang.name}`);
});

// output
Id: 0, Name: PHP
Id: 1, Name: Python
Id: 2, Name: C
Id: 3, Name: Go
Id: 4, Name: Swift
Give the gift of Audible

4. while Loop


var languages = [
    {id: 1, name: 'PHP', ratings: 8},
    {id: 2, name: 'Python', ratings: 9},
    {id: 3, name: 'C', ratings: 8},
    {id: 4, name: 'Go', ratings: 8},
    {id: 5, name: 'Swift', ratings: 7}
];

var i = 0;
while(i < languages.length) {
    console.log(languages[i].name)
    i++;
}

// output
PHP
Python
C
Go
Swift

5. do/while Loop


var languages = [
    {id: 1, name: 'PHP', ratings: 8},
    {id: 2, name: 'Python', ratings: 9},
    {id: 3, name: 'C', ratings: 8},
    {id: 4, name: 'Go', ratings: 8},
    {id: 5, name: 'Swift', ratings: 7}
];

var i = 0;
do {
    console.log(languages[i].name)
    i++
} while(i < languages.length)

// output
PHP
Python
C
Go
Swift
amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

What is the correct way to enqueue multiple CSS files in WordPress?WordPressWhat is a “promise” in JavaScript?JavaScriptThe Drupal flowDrupalWhat are Modules and Components in Angular?AngularHow to create an empty array in Swift?SwiftHow to create a simple text and image slider using CSS and JavaScript?CSS