I know this existing question , however, I'm only interested in simple javascript solutions (without external libraries like lodash).
What would be the cleanest way (including all the goodnesses of ES6 and beyond - like rest and distribution of objects, etc.) to get an object with a subset of details from another object in javascript?
Say I want to select foo , bar and baz from a source object. I currently have two solutions, I don't like any of them:
1.
const result = { foo: source.foo, bar: source.bar, baz: source.baz };
2.
const { foo, bar, baz } = source; const target = { foo, bar, baz };
The second option is shorter, but it pollutes the current execution area with some variables, and their list should be written twice in any case.
PS. I'm also not interested in incrementing Object.prototype with some helper method or calling some user-defined functions to achieve this.
source share