FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Compact II

20mins

Implement a function compact(obj) that takes an object and returns a new object with all falsey values (false, 0, "", null, undefined, and NaN) removed.

The function should:

  • Recursively remove falsey values from nested objects and arrays.
  • Return a new object without mutating the original.
  • Keep all truthy values intact.

Example

compact({ a: 0, b: 1, c: false, d: "", e: null, f: { x: undefined, y: 2 }, g: [0, 3, null] }); // { b: 1, f: { y: 2 }, g: [3] }

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests