Problem:
How can you write a function that selects specific properties from an object in the most concise manner in ES6?
Initial Solution:
The following approach uses destructuring and a simplified object literal to achieve this:
(v) => { let { id, title } = v; return { id, title }; }
Improved Solution:
A more streamlined solution, which also eliminates the repetition of property names, can be achieved through parameter destructuring:
({id, title}) => ({id, title})
This solution provides a more concise alternative while retaining the desired functionality.
Alternative Approaches:
function pick(o, ...props) { var has = p => o.propertyIsEnumerable(p), get = p => Object.getOwnPropertyDescriptor(o, p); return Object.defineProperties({}, Object.assign({}, ...props .filter(prop => has(prop)) .map(prop => ({prop: get(props)}))) ); }
The above is the detailed content of How to Concisely Extract Specific Properties from an Object in ES6?. For more information, please follow other related articles on the PHP Chinese website!