Today community session with batch fsr060523

1-How can you loop through an array of objects using a for loop?

2 Likes

for (var i = 0; i < productList.length; i++) {
for (var j in productList[i]){
console.log(productList[i][j];
}
}

2 Likes
To loop through an array of objects using a for loop based on the  example, 

```javascript
// Array of objects
var students = [
  { name: 'John', age: 18 },
  { name: 'Jane', age: 20 },
  { name: 'Bob', age: 22 }
];

// Loop through the array using a for loop
for (var i = 0; i < students.length; i++) {
  // Access the properties of each object
  var student = students[i];
  var name = student.name;
  var age = student.age;

  // Perform operations or display information
  console.log('Student Name:', name);
  console.log('Student Age:', age);
  console.log('----------------------');
}

In this example, we have an array called students that contains three objects representing student information. The for loop iterates through each object in the array. Inside the loop, we access the properties of each object using dot notation (student.name and student.age) and perform any desired operations or display information.

The loop will output the name and age of each student in the array. we can modify the code inside the loop to perform any specific operations or calculations based on our requirements.

1 Like
var numbers = [
 
    {
      "arr1" : [1,2,3,4,5,6,7,8,9,10]
    },
    {
      "arr2" : [1,2,3,4,5,6,7,8,9,10]
    },
    {
      "arr3" : [1,2,3,4,5,6,7,8,9,10]
    },
    {
      "strings" : ["a","b","c","i","o","u"]
    }
];

for(var i=0;i<numbers.length;i++){
    // console.log(i);
    if(i==2){
        for(var j in numbers[i]){
            document.write(numbers[i][j]+"<br>");
        }
   
}

else if(i==3){
    for(var j in numbers[i]){
        for(var y=0;y<numbers[i][j].length;y++){
            document.write(numbers[i][j][y].toUpperCase()+"<br>");
        }
    }
}
}
1 Like
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let empInfo = [
            emp1= {
                name: 'Krishna',
                email: 'ky789090@gmail.com',
                contact: '9599242522'
            },
            emp2= {
                name: 'Shiva',
                email: 'ahnsirk@gmail.com',
                contact: '7838434024'
            }
        ]
        for(let i=0; i<empInfo.length; i++){
            for(let k in empInfo[i]){
                console.log(empInfo[i][k]);
            }
        }
    </script>

</body>

</html>
1 Like

2-How can you iterate over the properties of an object using a for…in loop?

<script>
    var a = {key : "billa",key1 : "dav",key2 : "billa"}
    for(j in a){
        console.log(j)
    }
1 Like
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let empInfo = [
            emp1= {
                name: 'Krishna',
                email: 'ky789090@gmail.com',
                contact: '9599242522'
            },
            emp2= {
                name: 'Shiva',
                email: 'ahnsirk@gmail.com',
                contact: '7838434024'
            }
        ]
        for(let i=0; i<empInfo.length; i++){
            for(let k in empInfo[i]){
                console.log(empInfo[i][k]);
            }
        }
        for(let i=0; i<empInfo.length; i++){
            for(let k in empInfo[i]){
                if(k=='name'){
                    console.log(empInfo[i][k])
                }
            }
        }
    </script>

</body>

</html>

1 Like

var array = [
{ name: ‘John’, age: 25 },
{ name: ‘Jane’, age: 30 },
{ name: ‘Bob’, age: 35 }
];

for(var i=0; i<array.length;i++){
for(var j in array[i]){

console.log(array[i][j])

}
}

1 Like

var emp_info = [
{
name : “sam”,
email : “sam@gmail.com”,
contact : 98789
},
{
name : “peter”,
email : “peter@gmail.com”,
contact : 34567890
},

]
for(var i=0;i<emp_info.length;i++){
// console.log(emp_info[i])
for(var j in emp_info[i]){
document.write(emp_info[i][j])
}
}

1 Like
1 Like

const people = [
{ name: ‘Shivu’, age: 25 },
{ name: ‘Sam’, age: 30 },
{ name: ‘Smith’, age: 35 }
];

for (let i = 0; i < people.length; i++) {
const person = people[i];
console.log(Name: ${person.name}, Age: ${person.age});
}

1 Like
To iterate over the properties of an object using a `for...in` loop in JavaScript, we can follow these steps:

1. Define an object that we want to iterate over.
2. Use the `for...in` loop to loop over the object's properties.
3. Within the loop, access each property using the loop variable and perform the desired operations.

Here's an example that demonstrates how to iterate over the properties of an object using a `for...in` loop:

```javascript
var person = {
  name: "John",
  age: 30,
  gender: "male"
};

for (var prop in person) {
  console.log(prop + ": " + person[prop]);
}

In this example, we have an object person with properties such as name, age, and gender. We use the for...in loop to iterate over each property of the person object.

Within the loop, the prop variable represents the current property name. We access the corresponding property value using person[prop]. In each iteration, we log the property name and its value to the console using console.log().

The output of the above code will be:

name: John
age: 30
gender: male

The for...in loop iterates over all enumerable properties of the object, including properties inherited from its prototype chain. If we only want to iterate over the object’s own properties (excluding inherited properties), we can use the hasOwnProperty() method inside the loop as a safeguard:

for (var prop in person) {
  if (person.hasOwnProperty(prop)) {
    console.log(prop + ": " + person[prop]);
  }
}

By checking person.hasOwnProperty(prop), we ensure that only the object’s own properties are iterated over.

1 Like
var data1 = {
  "page": 1,
  "per_page": 6,
  "total": 12,
  "total_pages": 2,
  "users": [
    {
      id: 1,
      email: "george.bluth@reqres.in",
      first_name: "George",
      last_name: "Bluth",
      avatar: "https://reqres.in/img/faces/1-image.jpg%22"
    },
    {
      id: 2,
      email: "janet.weaver@reqres.in",
      first_name: "Janet",
      last_name: "Weaver",
      avatar: "https://reqres.in/img/faces/2-image.jpg%22"
    },
    {
      id: 3,
      email: "emma.wong@reqres.in",
      first_name: "Emma",
      last_name: "Wong",
      avatar: "https://reqres.in/img/faces/3-image.jpg%22"
    },
    {
      id: 4,
      email: "eve.holt@reqres.in",
      first_name: "Eve",
      last_name: "Holt",
      avatar: "https://reqres.in/img/faces/4-image.jpg%22"
    },
    {
      id: 5,
      email: "charles.morris@reqres.in",
      first_name: "Charles",
      last_name: "Morris",
      avatar: "https://reqres.in/img/faces/5-image.jpg%22"
    },
    {
      id: 6,
      email: "tracey.ramos@reqres.in",
      first_name: "Tracey",
      last_name: "Ramos",
      avatar: "https://reqres.in/img/faces/6-image.jpg%22"
    }
  ],
  "support": {

    "url": "https://reqres.in/#support-heading%22",
    "text" : "To keep ReqRes free, contributions towards server costs are appreciated!"
  }
};
// Q.s 1 read the user data all together such as id email first_name last_name avatar


//  for(var x in data1){
//     // console.log(data1[x]);
//     for(var y=0;y<data1[x].length;y++){
//         console.log(data1[x][y]);
//         for(var z in data1[x][y]){
//             document.write(z+":-----"+data1[x][y][z]+"<br>");
//         }
//         document.write("<br>*******************************************<br>")
//     }
    
//  }
2 Likes

const person = {
name: ‘Shivu’,
age: 25,
occupation: ‘Web developer’
};

for (let propertyName in person) {
const propertyValue = person[propertyName];
console.log(${propertyName}: ${propertyValue});
}

1 Like

3-How can you create a nested loop in JavaScript?

var n = [

{
  "arr1" : [1,2,3,4,5,6,7,8,9,10]
},
{
  "arr2" : [1,2,3,4,5,6,7,8,9,10]
},
{
  "arr3" : [1,2,3,4,5,6,7,8,9,10]
},
{
  "strings" : ["a","b","c","i","o","u"]
}

]
for(i = 0;i < n.length;i++){

for(j in n[i]){

if(j == "strings"){
  console.log(n[i][j])
  for(k = 0;k < n[i][j].length;k ++){
    console.log(n[i][j][k].toUpperCase())
  }
}

}
}

1 Like
To create a nested loop in JavaScript, we can use a combination of `for` loops. A nested loop consists of one loop inside another, allowing us to iterate over multiple dimensions or levels of data.

Here's an example that demonstrates how to create a nested loop:

```Example
for (var i = 1; i <= 3; i++) {
  for (var j = 1; j <= 3; j++) {
    console.log("i:", i, "j:", j);
  }
}

In this example, we have two for loops: an outer loop and an inner loop. The outer loop iterates over the variable i from 1 to 3, and the inner loop iterates over the variable j from 1 to 3.

Within the nested loops, we can perform any desired actions or operations using the values of i and j. In this case, we are logging the values to the console. The output will be:

i: 1 j: 1
i: 1 j: 2
i: 1 j: 3
i: 2 j: 1
i: 2 j: 2
i: 2 j: 3
i: 3 j: 1
i: 3 j: 2
i: 3 j: 3

As we can see, the inner loop completes one full iteration for each iteration of the outer loop. This creates a nested loop structure, allowing us to perform actions or operations on each combination of the loop variables.

We can add additional nested loops as needed, each inside the body of the previous loop, to create deeper levels of iteration. It’s important to control the loop variables appropriately and consider the complexity and efficiency of our code when working with nesstded loops.

1 Like

var emp_info = {
emp1 : {
name : “sam”,
email : “sam@gmail.com”,
contact : 98789
},
emp2 : {
name : “peter”,
email : “peter@gmail.com”,
contact : 34567890
},
emp3 : {
name : “tom”,
email : “tom@gmail.com”,
contact : 34567890
},
emp4 : {
name : “anil”,
email : “anil@gmail.com”,
contact : 34567890
}

}
for(var i=0;i<emp_info.length;i++){
// console.log(emp_info[i])
for(var j in emp_info[i]){
document.write(emp_info[i][j])
}
}

1 Like

Nested for loop

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let enterValue = parseInt(prompt('Enter Number to print triangle'));
        let print = "";
        for (let row = 1; row <= enterValue; row++) {
            for (let colOne = row; colOne <= enterValue; colOne++) {
                print = print + " ";
            }
            for (let col = 1; col <= 2 * row - 1; col++) {
                print = print + "*";
            }
            print = print + "\n";
        }
        console.log(print);
    </script>

</body>

</html>
1 Like