Omit keys from an object
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
sourceare 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
sourcedoes not have. - Return
{}whensourceis empty. - Do not mutate
source.
Stuck? The javascript course covers everything this challenge needs.
- 1.drops the listed keys
- 2.keeps the source ordering of the survivors
- 3.ignores keys that are not present
- 4.returns an empty object for an empty source
- 5.does not mutate the source
These assertions run in your browser the moment you hit Run. Pass all of them to claim the XP.
Discussion
Sign in to postNo comments yet. Be the first to say something.