How to get a subset of a JavaScript object's properties
To quickly obtain a subset of an object's properties, use the destructuring syntax with the rest operator:
In a more dynamic case, a reducer function can come in handy:
Comprehensive solutions for your daily needs
Destructuring assignment in IIFEs
An Immediately Invoked Function Expression (IIFE) provides an alternative way of creating a subset:
Utility libraries — The all-rounder Lodash
Why reinvent the wheel when Lodash already gives you a wheel, and a spare one.
The deadly duo: Spread and Object.entries
The spread syntax and Object.entries can tag team for an elegant solution:
Transform, roll out!
Ever feel like not only picking properties but also transforming them? There's a trick just for that.
Mapping entries like a pro
Object.entries, Object.fromEntries and map, make transformation as easy as ABC:
Strolling away from vendor dependencies
Say no more to vendor lock-in, with native JavaScript we can break these chains:
Pearls from the deep — complex scenarios
Facing special property keys or nested objects? Don't sweat! We got your back.
Special keys, special handling
If your properties have symbols or non-standard keys, they need special attention, like my cat when it sees a cucumber:
Digging into nested objects
Nested objects are like those Russian nesting dolls, there's always something within:
Best friend TypeScript
If you're a TypeScript aficionado, ensure your property picking isn't just typesafe, but also type-awesome:
Was this article helpful?