js for key value in object code example

Example 1: iterate object javascript

let obj = {
	key1: "value1",
	key2: "value2",
	key3: "value3",
	key4: "value4",
}
Object.entries(obj).forEach(([key, value]) => {
	console.log(key, value);
});

Example 2: iterate key value object javascript

'use strict';
// ECMAScript 2017
const object = {'a': 1, 'b': 2, 'c' : 3};
for (const [key, value] of Object.entries(object)) {
  console.log(key, value);
}

Example 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
*/

Example 4: javascript iterate object key values

Object.entries(obj).forEach(([key, value]) => {
	console.log(key, value);
});

Example 5: for key value in object javascript

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

Example 6: iterate over object javascript

// For a functional one-liner
Object.keys(pokemons).forEach(console.log);
// Bulbasaur
// Charmander
// Squirtle
// Pikachu