Omit some keys
A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.
The task
Return a new object with every own key of `obj` except the ones listed in `keys`. Names in `keys` that `obj` does not have are ignored. Keys whose value is `undefined` are kept. The copy is shallow (nested values are the same objects), and `obj` itself must not change.
What this exercise teaches
Building a new object from the entries you want is the opposite of pick and never touches the original. Deleting keys from obj would change the caller's data, and skipping undefined values would drop keys the caller may rely on.
A hint if you get stuck
Loop over `Object.keys(obj)` (or `Object.entries`) and copy the pairs whose key is not in `keys` into a fresh object. `keys.includes(k)` is enough for the check.
Try it now, free, no account needed
Every exercise on Scriptdrill is free to try. Open it on the practice page, solve it, and get instant feedback with a hint. The worked solution opens once you solve it or after two tries.
Open this exerciseMore exercises in Arrays and objects
- Group by a fieldFree
- Count by a fieldFree
- Remove duplicates by a fieldFree
- Split into chunksFree
- Zip two arraysFree
- Flatten one levelFree
- Pick some keysFree
- Sort by several keysFree
- Merge settingsFree
- Flatten to a depthFree
- Transpose a matrixFree
- Binary searchFree
- Two numbers that add upFree
- Values in both arraysFree
- Rotate an arrayFree