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,
nulland 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
Task 1, not checked yet. Make
flattenObjectpass the example tests.Task 2, not checked yet. Make
flattenObjectpass the hidden tests.
Task 1 of 2
Make flattenObject pass the example tests.