FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Deep Map

25mins

Implement a function deepMap(obj, fn) that recursively transforms all values in an object or array using the provided function.

The function should:

  • Accept an object or array as the first argument.
  • Accept a transformation function fn as the second argument.
  • Recursively apply the function to all nested values.
  • Return a new object/array without mutating the original.

Example

const obj = { a: 1, b: { c: 2, d: [3, 4] } }; const result = deepMap(obj, x => x * 2); console.log(result); // { a: 2, b: { c: 4, d: [6, 8] } }

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests