DEV Community

Temitope Ayodele
Temitope Ayodele

Posted on

3 2

Object.entries()

Object.entries() is used for listing all key-value pairs in an object. It accepts an object as a argument, and returns an array enumerating the key-value pairs of an object.

const obj = { 
  1: 'Israel', 
  2: 'Temi', 
  3: 'Miro' 
};
console.log(Object.entries(obj))

//Expected result: [ ["1", "Israel"], ["2", "Temi"], ["3", "Ayo"]]
Enter fullscreen mode Exit fullscreen mode

Another example:

const obj1 = {
  a: 'Hello',
  b: 28
};

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

//Expected result: 
// "a: Hello"
// "b: 28"
Enter fullscreen mode Exit fullscreen mode

If the argument passed is not an object, it causes TypeError,
If the key passed in the argument is not in the range of the property[key, value] pair, it causes RangeError.

Top comments (2)

Collapse
 
pabloabc profile image
Pablo Berganza

Nice post for beginners ❤

Just a tip, if you want to have syntax highlighting in your post you can just add the language name directly after the first three backquotes of the code block!

It would look like this:

console.log('hi')
Collapse
 
temmietope profile image
Temitope Ayodele

Thanks!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay