learn kro favicon img

Object.keys, values, entries

Let’s step away from the individual data structures and talk about the iterations over them.

In the previous chapter we saw methods map.keys()map.values()map.entries().

These methods are generic, there is a common agreement to use them for data structures. If we ever create a data structure of our own, we should implement them too.

They are supported for:

  • Map
  • Set
  • Array

Plain objects also support similar methods, but the syntax is a bit different.

Object.keys, values, entries

For plain objects, the following methods are available:

Please note the distinctions (compared to map for example):

MapObject
Call syntaxmap.keys()Object.keys(obj), but not obj.keys()
Returnsiterable“real” Array

The first difference is that we have to call Object.keys(obj), and not obj.keys().

Why so? The main reason is flexibility. Remember, objects are a base of all complex structures in JavaScript. So we may have an object of our own like data that implements its own data.values() method. And we still can call Object.values(data) on it.

The second difference is that Object.* methods return “real” array objects, not just an iterable. That’s mainly for historical reasons.

For instance:

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

Here’s an example of using Object.values to loop over property values:

Object.keys/values/entries ignore symbolic properties

Just like a for..in loop, these methods ignore properties that use Symbol(...) as keys.

Usually that’s convenient. But if we want symbolic keys too, then there’s a separate method Object.getOwnPropertySymbols that returns an array of only symbolic keys. Also, there exist a method Reflect.ownKeys(obj) that returns all keys.

Transforming objects

Objects lack many methods that exist for arrays, e.g. mapfilter and others.

If we’d like to apply them, then we can use Object.entries followed by Object.fromEntries:

  1. Use Object.entries(obj) to get an array of key/value pairs from obj.
  2. Use array methods on that array, e.g. map, to transform these key/value pairs.
  3. Use Object.fromEntries(array) on the resulting array to turn it back into an object.

For example, we have an object with prices, and would like to double them:

It may look difficult at first sight, but becomes easy to understand after you use it once or twice. We can make powerful chains of transforms this way.

Tasks

Sum the properties

importance: 5

There is a salaries object with arbitrary number of salaries.

Write the function sumSalaries(salaries) that returns the sum of all salaries using Object.values and the for..of loop.

If salaries is empty, then the result must be 0.

For instance:

Open a sandbox with tests.

solution

Or, optionally, we could also get the sum using Object.values and reduce:

Open the solution with tests in a sandbox.

Count properties

importance: 5

Write a function count(obj) that returns the number of properties in the object:

Try to make the code as short as possible.

P.S. Ignore symbolic properties, count only “regular” ones.

Open a sandbox with tests.

solution

Open the solution with tests in a sandbox.

Leave a Comment

Your email address will not be published. Required fields are marked *