LearnFrenzy
Sign in Take a free test

Objects · Predict the Output · Question 14

Swapping keys and values Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 14 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const o = { a: 1, b: 2 };
console.log(Object.fromEntries(Object.entries(o).map(([k, v]) => [v, k])));
  • A { 1: 'a', 2: 'b' }
  • B { a: 1, b: 2 }
  • C [ [ 1, 'a' ], [ 2, 'b' ] ]
  • D { '1': 'a', '2': 'b' } Correct
Correct Answer: Option D

The numbers become string keys. Node shows numeric-looking keys in quotes to make clear that they are strings.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links