Skip to curriculum
Jump to the editor

4.10. Object from key-value pairs, and back

Easy
Topics

This question is in

4. Array utilities
10–20 min

A form library reads a form's fields as a list of [name, value] pairs and needs them as an object to send. A URL's query string arrives as pairs too. Going the other way, a settings object has to become a list of rows to show in a table, or to sort and filter with array methods before it turns back into an object. Pairs and objects are two shapes of the same data, and moving between them has a few sharp edges.

Write fromPairs(pairs) and toPairs(object) in pairs.js, without Object.fromEntries or Object.entries.

Signature

JAVASCRIPT
function fromPairs(pairs)  // returns a new object
function toPairs(object)   // returns a new array of [key, value] arrays
  • pairs: an array of [key, value] arrays, or any other iterable of them, such as a Map.
  • toPairs lists the object's own, enumerable, string-keyed properties.

Requirements

fromPairs:

  1. It returns a new plain object (one that inherits from Object.prototype) with a property for each pair.
  2. When a key repeats, the later value wins, and the key keeps the position of its first appearance. Positions follow JavaScript's property order: integer-like keys ("2", "10") always come first in ascending order, then the other strings in the order they first appear, then symbols. So fromPairs([["b", 1], ["2", 2]]) lists "2" before "b".
  3. Keys become property names: numbers turn into strings, and symbols stay symbols.
  4. A pair whose key is "__proto__" makes an ordinary own property named "__proto__"; it never changes the object's prototype.
  5. A pair with no second item gives the value undefined.
  6. It accepts any iterable of pairs, such as a Map. Values are kept as they are, not copied.

toPairs:

  1. It returns [key, value] for each own enumerable string key, in the same order as Object.keys (whole-number keys first, in ascending order, then the others in the order they were added).
  2. It skips inherited properties, non-enumerable properties and symbol keys.
  3. For an array, the keys are the indexes as strings. An empty object gives [].

Both:

  1. fromPairs(toPairs(object)) rebuilds an object with the same own enumerable string keys and values, in the same order. Symbol keys, non-enumerable and inherited properties are not listed by toPairs (rule 8), so they are not rebuilt.
  2. Object.fromEntries and Object.entries are not used.

Examples

JAVASCRIPT
fromPairs([["name", "Ana"], ["age", 31]]);   // { name: "Ana", age: 31 }
fromPairs([["a", 1], ["b", 2], ["a", 3]]);   // { a: 3, b: 2 }
fromPairs(new Map([[1, "one"]]));            // { "1": "one" }
JAVASCRIPT
toPairs({ b: 2, a: 1, 10: "x", 2: "y" });
// [["2", "y"], ["10", "x"], ["b", 2], ["a", 1]]
JAVASCRIPT
const object = fromPairs([["__proto__", { admin: true }]]);
object.admin;                      // undefined: the prototype is unchanged
Object.keys(object);               // ["__proto__"]

Environment

  • Plain JavaScript in a browser page; the tests call the globals fromPairs and toPairs from pairs.js.
  • Object.fromEntries and Object.entries are removed while the tests run. Other built-ins, such as Object.keys and Object.defineProperty, may be used.

Hints

  1. What does object["__proto__"] = value do on a plain object? Is there a way to create a property that skips that rule?
  2. Which built-in already lists exactly the keys toPairs needs, in the right order?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…