JS/TS
  • JavaScript Development
  • JS Principles
    • JS Principles
      • Primitive data types
      • typeof operator
      • Scope
      • Hoisting
      • IIFE
      • Closure
      • Anonymous functions in JS
      • Conditional (ternary) operator
        • Coercion vs Conversion
      • Event-driven programming
      • Factory Function
      • JSON.stringify()
      • Strict mode
      • super() keyword
      • What are memory leaks?
      • Micro-tasks within an event loop (Summary)
      • Macro-tasks within an event loop (Summary)
      • null vs undefined
    • Memory Management
    • Advanced function concepts
      • Impure vs Pure Functions
      • Factory functions
  • JavaScript Objects & Arrays
    • Introducing JavaScript objects
      • Build-in objects
        • isNaN()
      • RegExp
        • RegExp.prototype.test()
      • String
        • String.prototype.split()
        • String.prototype.slice()
      • Objects
        • Object.assign()
        • Object.create()
        • Object.defineProperties()
        • Object.defineProperty()
        • Object.entries()
        • Object.freeze()
        • Object.getOwnPropertyNames()
        • Object.getPrototypeOf()
        • Object.isFrozen()
        • Object.isSealed()
        • Map
      • Standard built-in methods to work with Arrays
        • Array.of()
        • Array.prototype.concat()
        • Array.prototype.every()
        • Array.prototype.filter()
        • Array.prototype.find()
        • Array.prototype.findIndex()
        • Array.prototype.forEach()
        • Array.prototype.join()
        • Array.prototype.map()
        • Array.prototype.pop()
        • Array.prototype.shift()
        • Array.prototype.reverse()
        • Array.prototype.some()
        • Array.prototype.sort()
        • Array.prototype.splice()
        • Array.prototype.unshift()
        • Array.prototype.includes()
        • Array.prototype.flatMap()
      • Prototypal inheritance
        • Inheritance with the prototype chain
        • Inheriting "methods"
  • JavaScript Mid
    • JavaScript & ES
      • Arrow Function
      • Anonymous Function
      • Callbacks
      • Promises
      • var, let, and const
      • Fetch API (function)
      • Fetch API
      • Synchronous vs Asynchronous
      • Encapsulation
      • Destructuring assignment
      • call() - apply() - bind()
      • 'This' keyword
      • Functional Programming
  • Browser
    • Event-driven programming
  • TypeScript
    • The TypeScript Handbook
      • Basic Types
      • Interfaces
      • Functions
      • Literal Types
      • Unions and Intersection Types
      • Classes
      • Enums
      • Generics
      • Implements vs extends
  • Hackerrank Practices
    • Practices and examples
  • JS Math
    • Mathematical
      • JavaScript | Math.E() function
      • Math.abs( ) Method
      • Math.ceil( ) function
      • Math floor()
      • Math.imul( ) Function
      • Math log( ) Method
      • Math max()/min() Method
      • Math pow( ) Method
      • Math.sign( ) Function
      • Math sqrt( ) Method
Powered by GitBook
On this page
  • for-ofloop
  • for-in loop
  • Question
  • break vs return

Was this helpful?

  1. JavaScript Objects & Arrays
  2. Introducing JavaScript objects
  3. Objects

Object.entries()

PreviousObject.defineProperty()NextObject.freeze()

Last updated 3 years ago

Was this helpful?

The Object.entries() method returns an array of a given object's own enumerable string-keyed property [key, value] pairs, in the same order as that provided by a loop. (The only important difference is that a for...in loop enumerates properties in the prototype chain as well).

The order of the array returned by Object.entries() does not depend on how an object is defined. If there is a need for certain ordering, then the array should be sorted first, like Object.entries(obj).sort((a, b) => b[0].localeCompare(a[0]));.

for-ofloop

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

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

// expected output:
// "a: somestring"
// "b: 42"
// order is not guaranteed

for-in loop

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

for (const [key, value] of Object.entries(myObject)) {
    for (const keyItem in key) {
        console.log(key[keyItem]); // Expected: a & b
    }
}

Practical example with for, for-in and for-of

function printLogHandler() {
  for (let i = 0; i < 3; i++) {
    console.log('------------');
  }
  let i = 0;
  for (const logEntry of battleLog) {
    console.log(`#${i}`);
    for (const key in logEntry) {
      console.log(`${key} => ${logEntry[key]}`);
    }
    i++;
  }
}

// Expected output
// #0
// event => PLAYER_ATTACK
// value => 0.33673722139444084
// finalMonsterHealth => 99.66326277860556
// finalPlayerHealth => 100
// target => MONSTER
// #1
// event => MONSTER_ATTACK
// value => 11.435543505902086
// target => PLAYER
// finalMonsterHealth => 99.66326277860556
// finalPlayerHealth => 88.56445649409791
// #2
// event => PLAYER_ATTACK
// value => 2.052553803920303
// finalMonsterHealth => 97.61070897468525
// finalPlayerHealth => 88.56445649409791
// target => MONSTER

while loop with an example

(function () {
    let randomNumbers = [];
    let finished;
    while (!finished) {
        const rndNumber = Math.random();
        randomNumbers.push(rndNumber);
        if (rndNumber > 0.5) {
            finished = true;
            console.log(randomNumbers);
        }
    }
})();
// This example uses a IIFE

Question

What's the main difference between for-of and for-in loops?

Answer: for-of are used for arrays and for-in for objects

break vs return

break is used when you want to exit from the loop, while return is used to go back to the step where it was called or to stop further execution.

(function () {
    const myArray = [1, 2, 3, 4, 5, 6];
    for (let i = 0; i < myArray.length; i++) {
        if (myArray[i] === 3) {
            console.log('This uses return');
            return;
        }
    }

    for (let i = 0; i < myArray.length; i++) {
        if (myArray[i] === 3) {
            console.log('This uses break');
            break;
        }
    }
})();

// Expected:
// This uses return
// This will never get to the break statement
(function () {
    const myArray = [1, 2, 3, 4, 5, 6];
    // for (let i = 0; i < myArray.length; i++) {
    //     if (myArray[i] === 3) {
    //         console.log('This uses return');
    //         return;
    //     }
    // }

    for (let i = 0; i < myArray.length; i++) {
        if (myArray[i] === 3) {
            console.log('This uses break');
            break;
        }
    }
})();
// Expected result: 
// This uses break
for...in