Build lodash.omitBy and lodash.pickBy with Object.fromEntries

Jamund Ferguson
InstructorJamund Ferguson

Share this video with your friends

Send Tweet

_.omitBy and its sibling _.pickBy are popular utilities which apply filter-like functionality to objects. Both of them accept a function to determine if a given entry in the object should be included in the output object. This lesson will demonstrate an approach to build both of these utilities from a single function using the combo of Object.entries and Object.fromEntries.

Object.entries converts an object into an array of key, value pairs. We couple that with the native Array filter method, to create a generate filter() method that works on objects. Once we've filtered our object we use Object.fromEntries to convert the array back into an object. This works great for both omitBy and pickBy.


Object.fromEntries is part of the ES2019 spec and may need a polyfill to work depending on your environment.