FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Use Object

20mins

Implement a hook useObject(initialObject) that provides a reactive object with utility functions.

The hook should:

  • Accept an optional initial object to populate the state.
  • Provide methods to get, set, delete keys.
  • Provide a method clear() to remove all keys.
  • Provide a method keys(), values(), entries() to access the current state.
  • Updates to the object should be reflected immediately through these methods.

Example

const obj = useObject({ a: 1, b: 2 }); obj.set("c", 3); obj.get("a"); // 1 obj.delete("b"); obj.has("b"); // false

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests