FSR060523 :- Practice Problem | Javascript | Object | Array


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"
    },
    {
      id: 2,
      email: "janet.weaver@reqres.in",
      first_name: "Janet",
      last_name: "Weaver",
      avatar: "https://reqres.in/img/faces/2-image.jpg"
    },
    {
      id: 3,
      email: "emma.wong@reqres.in",
      first_name: "Emma",
      last_name: "Wong",
      avatar: "https://reqres.in/img/faces/3-image.jpg"
    },
    {
      id: 4,
      email: "eve.holt@reqres.in",
      first_name: "Eve",
      last_name: "Holt",
      avatar: "https://reqres.in/img/faces/4-image.jpg"
    },
    {
      id: 5,
      email: "charles.morris@reqres.in",
      first_name: "Charles",
      last_name: "Morris",
      avatar: "https://reqres.in/img/faces/5-image.jpg"
    },
    {
      id: 6,
      email: "tracey.ramos@reqres.in",
      first_name: "Tracey",
      last_name: "Ramos",
      avatar: "https://reqres.in/img/faces/6-image.jpg"
    }
  ],
  "support": {
    "url": "https://reqres.in/#support-heading",
    "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
// Q.s 2 read only email
// Q.s 3 read url

3 Likes

//ANS 1

var users = data1.users;

for (var i = 0; i < users.length; i++) {
var user = users[i];
var id = user.id;
var email = user.email;
var firstName = user.first_name;
var lastName = user.last_name;
var avatar = user.avatar;

document.write(“User ID: " + id+”
“);
document.write(“Email: " + email+”
”);
document.write(“First Name: " + firstName+”
“);
document.write(“Last Name: " + lastName+”
”);
document.write(“Avatar: " + avatar+”
");
}

2 Likes
var users = data1.users;

for (var i = 0; i < users.length; i++) {
  var user = users[i];
  var id = user.id;
  var email = user.email;
  var firstName = user.first_name;
  var lastName = user.last_name;
  var avatar = user.avatar;

  console.log("User ID:", id);
  console.log("Email:", email);
  console.log("First Name:", firstName);
  console.log("Last Name:", lastName);
  console.log("Avatar:", avatar);
  console.log("------");
}

Output:

User ID: 1
Email: george.bluth@reqres.in
First Name: George
Last Name: Bluth
Avatar: https://reqres.in/img/faces/1-image.jpg

User ID: 2
Email: janet.weaver@reqres.in
First Name: Janet
Last Name: Weaver
Avatar: https://reqres.in/img/faces/2-image.jpg

User ID: 3
Email: emma.wong@reqres.in
First Name: Emma
Last Name: Wong
Avatar: https://reqres.in/img/faces/3-image.jpg

User ID: 4
Email: eve.holt@reqres.in
First Name: Eve
Last Name: Holt
Avatar: https://reqres.in/img/faces/4-image.jpg

User ID: 5
Email: charles.morris@reqres.in
First Name: Charles
Last Name: Morris
Avatar: https://reqres.in/img/faces/5-image.jpg

User ID: 6
Email: tracey.ramos@reqres.in
First Name: Tracey
Last Name: Ramos
Avatar: https://reqres.in/img/faces/6-image.jpg

2 Likes

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
},
{
id: 2,
email: “janet.weaver@reqres.in”,
first_name: “Janet”,
last_name: “Weaver”,
avatar: “https://reqres.in/img/faces/2-image.jpg
},
{
id: 3,
email: “emma.wong@reqres.in”,
first_name: “Emma”,
last_name: “Wong”,
avatar: “https://reqres.in/img/faces/3-image.jpg
},
{
id: 4,
email: “eve.holt@reqres.in”,
first_name: “Eve”,
last_name: “Holt”,
avatar: “https://reqres.in/img/faces/4-image.jpg
},
{
id: 5,
email: “charles.morris@reqres.in”,
first_name: “Charles”,
last_name: “Morris”,
avatar: “https://reqres.in/img/faces/5-image.jpg
},
{
id: 6,
email: “tracey.ramos@reqres.in”,
first_name: “Tracey”,
last_name: “Ramos”,
avatar: “https://reqres.in/img/faces/6-image.jpg
}
],
“support”: {
“url”: “Reqres - A hosted REST-API ready to respond to your AJAX requests”,
“text”: “To keep ReqRes free, contributions towards server costs are appreciated!”
}
};

var users = data1.users;

for (var i = 0; i < users.length; i++) {
var user = users[i];
var id = user.id;
var email = user.email;
var firstName = user.first_name;
var lastName = user.last_name;
var avatar = user.avatar;

console.log("User " + (i + 1) + ":");
console.log("ID: " + id);
console.log("Email: " + email);
console.log("First Name: " + firstName);
console.log("Last Name: " + lastName);
console.log("Avatar: " + avatar);
console.log("------------------");

}

2 Likes

Answer-2:

var data1 = {
  
};

// Read only email with nested for loops
for (var i = 0; i < data1.users.length; i++) {
  for (var prop in data1.users[i]) {
    if (prop === "email") {
      console.log("Email:", data1.users[i][prop]);
    }
  }
}

Output:

Email: george.bluth@reqres.in
Email: janet.weaver@reqres.in
Email: emma.wong@reqres.in
Email: eve.holt@reqres.in
Email: charles.morris@reqres.in
Email: tracey.ramos@reqres.in

2 Likes

var users = data1.users;

for (var i = 0; i < users.length; i++) {
var user = users[i];
var email = user.email;

console.log("Email: " + email);
}

2 Likes

var users = data1.users;

for (var i = 0; i < users.length; i++) {
var user = users[i];
var avatar = user.avatar;

console.log("Avatar URL: " + avatar);
}

2 Likes

Answer-3:

var data1 = {
  // Data object 
};

// Reading URL with nested for loops
for (var i = 0; i < data1.users.length; i++) {
  for (var prop in data1.users[i]) {
    if (prop === "avatar") {
      console.log("URL:", data1.users[i][prop]);
    }
  }
}

Output:

URL: https://reqres.in/img/faces/1-image.jpg
URL: https://reqres.in/img/faces/2-image.jpg
URL: https://reqres.in/img/faces/3-image.jpg
URL: https://reqres.in/img/faces/4-image.jpg
URL: https://reqres.in/img/faces/5-image.jpg
URL: https://reqres.in/img/faces/6-image.jpg

2 Likes

var users = data1.users;

for (var i = 0; i < users.length; i++) {
var user = users[i];
var id = user.id;
var email = user.email;
var firstName = user.first_name;
var lastName = user.last_name;
var avatar = user.avatar;

document.write(“User " + id + “:”+”
“);
document.write(“Email: " + email+”
”);
document.write(“First Name: " + firstName+”
“);
document.write(“Last Name: " + lastName+”
”);
document.write(“Avatar URL: " + avatar+”
“);
document.write(”");
}

1 Like

Answer-1
METHOD-2:

var data1 = {
  // Data object 
};

// Read user data with nested for loops
for (var i = 0; i < data1.users.length; i++) {
  var user = data1.users[i];
  for (var prop in user) {
    console.log(prop + ": " + user[prop]);
  }
  console.log("----------");
}

Output:

id: 1
email: george.bluth@reqres.in
first_name: George
last_name: Bluth
avatar: https://reqres.in/img/faces/1-image.jpg

id: 2
email: janet.weaver@reqres.in
first_name: Janet
last_name: Weaver
avatar: https://reqres.in/img/faces/2-image.jpg

id: 3
email: emma.wong@reqres.in
first_name: Emma
last_name: Wong
avatar: https://reqres.in/img/faces/3-image.jpg

id: 4
email: eve.holt@reqres.in
first_name: Eve
last_name: Holt
avatar: https://reqres.in/img/faces/4-image.jpg

id: 5
email: charles.morris@reqres.in
first_name: Charles
last_name: Morris
avatar: https://reqres.in/img/faces/5-image.jpg

id: 6
email: tracey.ramos@reqres.in
first_name: Tracey
last_name: Ramos
avatar: https://reqres.in/img/faces/6-image.jpg

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"
      },
      {
        id: 2,
        email: "janet.weaver@reqres.in",
        first_name: "Janet",
        last_name: "Weaver",
        avatar: "https://reqres.in/img/faces/2-image.jpg"
      },
      {
        id: 3,
        email: "emma.wong@reqres.in",
        first_name: "Emma",
        last_name: "Wong",
        avatar: "https://reqres.in/img/faces/3-image.jpg"
      },
      {
        id: 4,
        email: "eve.holt@reqres.in",
        first_name: "Eve",
        last_name: "Holt",
        avatar: "https://reqres.in/img/faces/4-image.jpg"
      },
      {
        id: 5,
        email: "charles.morris@reqres.in",
        first_name: "Charles",
        last_name: "Morris",
        avatar: "https://reqres.in/img/faces/5-image.jpg"
      },
      {
        id: 6,
        email: "tracey.ramos@reqres.in",
        first_name: "Tracey",
        last_name: "Ramos",
        avatar: "https://reqres.in/img/faces/6-image.jpg"
      }
    ],
    "support": {
      "url": "https://reqres.in/#support-heading",
      "text": "To keep ReqRes free, contributions towards server costs are appreciated!"
    }
  }
//   Read the user data all together such as id email first_name last_name avatar

  var users = data1.users;

  for (var j = 0; j < users.length; j++) {
    var user = users[j];
    var id = user.id;
    var email = user.email;
    var firstName = user.first_name;
    var lastName = user.last_name;
    var avatar = user.avatar;
  
    console.log("User ID:", id);
    console.log("Email:", email);
    console.log("First Name:", firstName);
    console.log("Last Name:", lastName);
    console.log("Avatar:", avatar);
    console.log("------");
}

// Read the user email from the data1

var data1;
for (var k=0; k<data1.users.length;k++) {
    for (var l in data1.users[k]) {
        if(l == "email"){
            console.log(data1.users[k][l]);
            console.log("------");    }
    }
}

// Read the user avatar  from the data1
var data1;
for (var m=0; m<data1.users.length;m++) {
    for (var n in data1.users[m]) {
        if(n == "avatar"){
            console.log(data1.users[m][n]);
            console.log("------");    }
    }
}
1 Like

for (a in data1) {
if (a == “user”) {
for (b in data1[a]) {
for (c in data1[a][b]) {
document.write(data1[a][b][c] + “
”)
}
document.write(“
” + “********************” + “
”)
}
}
}