Answers for "foreach on objest in js"

10

foreach object javascript

const students = {
  adam: {age: 20},
  kevin: {age: 22},
};

Object.entries(students).forEach(student => {
  // key: student[0]
  // value: student[1]
  console.log(`Student: ${student[0]} is ${student[1].age} years old`);
});
/* Output:
Student: adam is 20 years old
Student: kevin is 22 years old
*/
Posted by: Guest on September-30-2020
4

object foreach

const obj = {
  name: 'Jean-Luc Picard',
  rank: 'Captain'
};

// Prints "name Jean-Luc Picard" followed by "rank Captain"
Object.keys(obj).forEach(key => {
  console.log(key, obj[key]);
});
Posted by: Guest on October-27-2020
3

foreach object javascript

/* Answer to: "foreach object javascript" */

const games = {
  "Fifa": "232",
  "Minecraft": "476"
  "Call of Duty": "182"
};

Object.keys(games).forEach((item, index, array) => {
  let msg = `There is a game called ${item} and it has sold ${games[item]} million copies.`;
  console.log(msg);
});

/*
  The foreach statement can be used in many ways and with object can make
  development a lot easier.
  
  A link for for more information on this can be found below and in the source:
  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
*/
Posted by: Guest on December-30-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language