Answers for "js foreach object key"

6

js loop through object

const obj = { a: 1, b: 2 };

Object.keys(obj).forEach(key => {
	console.log("key: ", key);
  	console.log("Value: ", obj[key]);
} );
Posted by: Guest on November-04-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
1

foreach key value javascript

const object1 = {
  a: 'somestring',
  b: 42
};

for (let [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

// expected output:
// "a: somestring"
// "b: 42"
// order is not guaranteed
Posted by: Guest on May-11-2020
0

iterate over object javascript

// For a functional one-liner
Object.keys(pokemons).forEach(console.log);
// Bulbasaur
// Charmander
// Squirtle
// Pikachu
Posted by: Guest on November-07-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language