31

What's the difference between Object.entries and Object.keys? In which case should I use one or the other one?

3
  • 1
    One gives you only the keys the other one the keys and values. You'd use one when you want only the keys, the other if you want the keys and values. Not sure what is unclear about this.
    – VLAZ
    Mar 9, 2019 at 14:41
  • 7
    The difference can pretty easily be seen by reading documentation on the methods: Object.keys Object.entries Mar 9, 2019 at 14:42
  • 11
    Ah SO. This question is well worded, easy to understand, and helpful, yet still closed with downvotes...
    – Zock77
    Aug 2, 2019 at 19:58

5 Answers 5

24

Object.keys returns only the own property names and works for ES5.

Object.entries returns an array of arrays with key and value and works from ES6.

If you need only keys or like to filter the keys, then take Object.keys, otherwise Object.entries.

1
  • 1
    Object.keys is really old and works from the stone age until now. Oct 30, 2020 at 13:14
15

Object.keys(obj) – returns an array of keys.

Object.entries(obj) – returns an array of [key, value] pairs.

Consider the below example.

 let user = {
 name: "John",
 age: 30
};

Object.keys(user) = ["name", "age"]

Object.entries(user) = [ ["name","John"], ["age",30] ]

When you want a key, value pair, you would use Object.entries. When you just want the key, you would use Object.keys.

3

Object.keys gives you an array of the keys in that object. where as Object.entries returns an array of arrays containing key value pair.

const object = { foo: 'bar', baz: 42 };
console.log(Object.keys(object));
console.log(Object.entries(object));

3

Both methods are used to iterate over Objects in JavaScript. The difference is:

Object.Keys -> will return an array of keys of objects.

Object.entries -> will return an array of arrays, where each array contains key and its value.

Note: - Objects in JavaScript include arrays and strings too.

Let’s understand behaviour of both methods with examples:

Example with an object literal:

const obj = {
   firstName : 'Kane',
   lastName : 'Williamson'
}

// Object.keys(obj) will give you:
[ 'firstName', 'lastname' ]

// Object.entries(obj) will give you:
[ [ 'firstName ', 'Kane' ], [ 'lastName ', 'Williamson' ] ]

An example with an array:

const arr = ['hello', 'john'];

// Object.keys(arr) will give you:
[ '0', '1' ] // note index are returned as strings

// Object.entries(arr) will give you:
[ [ '0', 'hello' ], [ '1', 'john' ] ]

An example with String:

const str = 'I am a String';

// Object.keys(str) will give you:
[
  '0',  '1', '2',  '3',
  '4',  '5', '6',  '7',
  '8',  '9', '10', '11',
  '12'
] // Note index are returned as strings

// Object.entries(str) will give you:
[
  [ '0', 'I' ],  [ '1', ' ' ],
  [ '2', 'a' ],  [ '3', 'm' ],
  [ '4', ' ' ],  [ '5', 'a' ],
  [ '6', ' ' ],  [ '7', 'S' ],
  [ '8', 't' ],  [ '9', 'r' ],
  [ '10', 'i' ], [ '11', 'n' ],
  [ '12', 'g' ]
]
2

The former returns a list of [key, values] while the latter only returns a list of keys.

The former allows you to trivially create a mapObject :

const mapObject = (obj, proj) => Object.entries(obj).reduce((r, [k, v]) => ({ ...r, [k]: proj(v) }), {});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.