AniUI Academy

Omit keys from an object

easyArrays & Objects+150 XPPractice

Before logging a request body or writing a record to analytics, you want everything except a handful of fields. Naming what to remove is often shorter and safer than naming what to keep.

Implement omit(source, keys). source is a plain object and keys is an array of property names to drop. Return a new object holding every own property of source whose name is not in keys. The remaining keys keep the relative order they had in source. Names in keys that source does not have are simply ignored. Never modify source.

Arguments

sourceObject
The plain object to copy keys from.
keysArray<string>
The property names to drop. Names not in source are ignored.

Returns

Object — A new object holding every own property of source whose name is not in keys, in the original order.

Examples

omit({ a: 1, b: 2, c: 3 }, ["b"]);
// { a: 1, c: 3 }
omit({ a: 1 }, ["missing"]);  // { a: 1 } — unknown names are ignored
omit({}, ["a"]);              // {}

What it has to do

  • Return a new object without the listed keys.
  • Keep the remaining keys in the same relative order as in source.
  • Ignore listed keys that source does not have.
  • Return {} when source is empty.
  • Do not mutate source.
Asked atMetaShopify

Stuck? The javascript course covers everything this challenge needs.

Try it yourself
Loading playground...
5 tests · +150 XP

Discussion

Sign in to post
Sign in to join the conversation.Sign in

No comments yet. Be the first to say something.