Course lesson
Build lodash.omit from Scratch with forEach and for...in
.omit and its sibling .pick are popular utilities which apply filter-like functionality to objects. This lesson will demonstrate two techniques for building the functionality found in .omit.
- Duration
- 2 min
- Access
- Free
- Transcript
- Available
_.omit and its sibling _.pick are popular utilities which apply filter-like functionality to objects. This lesson will demonstrate two techniques for building the functionality found in _.omit.
- Cloning the object with
Object.assignand removing unwanted keys withdelete - Using
for...inand.includesto manually copy keys into aresultobject
Notes:
In modern JavaScript environments instead of Object.assign({}, obj) you can also use { ...obj } which will have the same effect of a shallow copy.
This lesson will not cover how to emulate _.omit's support for taking either an array or multiple arguments for its keys, but our video on building _.pick from scratch covers one approach.