for key of object javascript code example

Example 1: javascript for key in object

// iterates over all enumerable properties of an object that are 
// keyed by strings (ignoring ones keyed by Symbols), 
// including inherited enumerable properties.

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

// expected output:
// "a: 1"
// "b: 2"
// "c: 3"

Example 2: for key value in object javascript

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

Example 3: js variable for key obj

{
    [yourKeyVariable]: someValueArray,
}