cf.completefrontend

Write the codeMedium

Write flattenObject(object) to turn a nested object into a single level with dotted keys.

{ a: { b: 1, c: [2, 3] } } becomes { "a.b": 1, "a.c.0": 2, "a.c.1": 3 }. Recurse into plain objects and arrays; anything else (null, dates, class instances, functions) is a value. Keep empty objects and arrays as values, so no key disappears.

Write this
function flattenObject(object) {
  // your code
}
 
// flattenObject({ user: { name: "Ada", tags: ["math"] }, active: true });
// // { "user.name": "Ada", "user.tags.0": "math", active: true }

Solve it in the editor, with tests

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…