How to use for…of the loop In Javascript

Accessing values from the JSON array by Iterating through each element of the JSON array. for…of statement iterates over the values of an iterable object (like Array, Map, Set, arguments object, …,etc), executing statements for each value of the object.

Syntax:

for ( variable of iterableObjectName) {
// code block to be executed
}

Example: Using for…of loop to access value.

Javascript




let jsonArray = [
    {
        "ComapnyName": "w3wiki",
        "Location": "Noida"
    },
    {
        "Courses": [
            "DSA self paced course",
            "DevOps Boot camp",
            "GATE prepration course"
        ],
        "Topics": [
            "Web Devlopment",
            "Machine Learning",
            "Artifical Intelligence"
        ]
    }
];
 
for (let element of jsonArray) {
    console.log(element);
}


Output

{ ComapnyName: 'w3wiki', Location: 'Noida' }
{
  Courses: [
    'DSA self paced course',
    'DevOps Boot camp',
    'GATE prepration course'
  ],
  Topics: [ 'Web Devlopment', 'Machine Learnin...

How to Get a Value from a JSON Array in JavaScript ?

We are going to iterate or access the value of a JSON array in JavaScript. There are several ways and techniques to access values from the JSON Array which are explained in detail below and are further divided into several possible techniques.

Table of Content

  • Using the array[index] method
  • Using the array[key] method
  • Using the array.slice method
  • Using for…of the loop
  • Using forEach()
  • Using the map() method
  • Using filter() method
  • Using the find() method
  • Using findIndex() method
  • Using some() method
  • Using recursive traversal of JSON array
  • Using the custom reduce method
  • Using custom classes

Similar Reads

Using the array[index] method

Accessing JSON array values using the numerical indexes. we can directly access the JSON array by accessing their indexes....

Using the array[key] method

...

Using the array.slice method

Accessing JSON array values within the JSON array elements using specific corresponding keys....

Using for…of the loop

...

Using forEach()

Extracting JSON arrays portion of values using slice method. slice() method returns a new array containing a portion of the array on which it is implemented. The original remains unchanged....

Using the map() method

...

Using filter() method

Accessing values from the JSON array by Iterating through each element of the JSON array. for…of statement iterates over the values of an iterable object (like Array, Map, Set, arguments object, …,etc), executing statements for each value of the object....

Using the find() method

...

Using findIndex() method

Extracting values from the JSON arrays by executing a function for each element of the array. forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array....

Using some() method

...

Using recursive traversal of JSON array

Accessing values from the JSON array by creating a new modified array from the existing JSON array values. map() method creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method....

Using the custom reduce method

...

Using custom classes

Extracting the values from the JSON array by creating a new array with the filtered elements from the original existing array. filter() Method is used to create a new array from a given array consisting of only those elements from the given array that satisfy a condition set by the argument method....