Practice

Problem 8 of 12

Problem · Medium · 25 min

Flatten a config

  • Medium
  • Recursion
  • Objects

A config file can hold objects inside objects. Write flattenObject(object). It returns a new object that has no nested objects. Each key of the result is the path of keys to a value, joined with dots.

For example, { server: { port: 80, tls: { on: true } }, name: "web" } becomes { "server.port": 80, "server.tls.on": true, "name": "web" }.

  • Only a plain object that has at least one key is opened. All other values stay as they are: numbers, strings, booleans, null and arrays. Do not look inside arrays.
  • An empty object is a value too. { a: {} } becomes { "a": {} }.
  • A key never holds a dot, and no two paths are the same.

Examples

Call Result
flattenObject({ server: { port: 80, tls: { on: true } }, name: 'web' }) { 'server.port': 80, 'server.tls.on': true, name: 'web' }
flattenObject({ a: 1 }) { a: 1 }
flattenObject({}) {}

Tasks

2 to do

  1. Task 1, not checked yet. Make flattenObject pass the example tests.

  2. Task 2, not checked yet. Make flattenObject pass the hidden tests.

Task 1 of 2

Make flattenObject pass the example tests.

All tasks

main.js

Ctrl+Enter runs your code. Ctrl+Shift+Enter runs all the tests. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.