Answers for "js foreach loop a object"

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
1

foreach object javascript

const obj = {
  a: "aa",
  b: "bb",
  c: "cc",
};
//This for loop will loop through all keys in the object.
// You can get the value by calling the key on the object with "[]"
for(let key in obj) {
  console.log(key);
  console.log(obj[key]);
}

//This will return the following:
// a
// aa
// b
// bb
// c
// cc
Posted by: Guest on October-07-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language