How to Extract and Copy Object Properties in JavaScript
Suppose you have an object named elmo with various properties. You wish to create a new object containing only a specified subset of these properties.
Object Destructuring and Property Shorthand Method:
To achieve this using object destructuring and property shorthand, follow these steps:
Example:
const elmo = { color: 'red', annoying: true, height: 'unknown', meta: { one: '1', two: '2'} }; const subset = (({ color, height }) => ({ color, height }))(elmo); console.log(subset); // { color: 'red', height: 'unknown' }
This method allows you to create a new object named subset containing only the color and height properties from the original elmo object.
The above is the detailed content of How to Extract Specific Object Properties in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!