EasyJavaScriptTypeScript

pickProps & omitProps

ReactObjectsProps

Implement two utilities:

  • pickProps(obj, keys) — returns a new object with only the listed keys.
  • omitProps(obj, keys) — returns a new object excluding the listed keys.

solve(obj, pickKeys, omitKeys) returns { picked, omitted }.

These are used in React to separate "own" props from those forwarded to a
child component.

Sample tests

Test #1Pick a,c — omit b
Input: [{"a":1,"b":2,"c":3},["a","c"],["b"]]
Output: {"picked":{"a":1,"c":3},"omitted":{"a":1,"c":3}}
Test #2Pick x, omit x
Input: [{"x":10,"y":20},["x"],["x"]]
Output: {"picked":{"x":10},"omitted":{"y":20}}
Test #3Pick nothing, omit all
Input: [{"a":1,"b":2},[],["a","b"]]
Output: {"picked":{},"omitted":{}}
Test #4Real-world prop splitting
Input: [{"id":1,"name":"Alice","role":"admin"},["id","name"],["role"]]
Output: {"picked":{"id":1,"name":"Alice"},"omitted":{"id":1,"name":"Alice"}}
Test #5Pick all, omit none
Input: [{"a":1,"b":2,"c":3,"d":4},["a","b","c","d"],[]]
Output: {"picked":{"a":1,"b":2,"c":3,"d":4},"omitted":{"a":1,"b":2,"c":3,"d":4}}