Scriptdrill

Level 2: Arrays and objects Exercise 25 of 76 Free

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 exercise

More exercises in Arrays and objects