Elegant way to copy only a part of an object

  • A+
Category:Languages

I would like to create an new object from a bigger one, by copying only a few properties from it. All the solutions I know are not very elegant, I wonder if there is a better choice, native if possible (no additional function like at the end of the following code)?

Here is what I usually do for now:

// I want to keep only x, y, and z properties: let source = {     x: 120,     y: 200,     z: 150,     radius: 10,     color: 'red', };  // 1st method (not elegant, especially with even more properties): let coords1 = {     x: source.x,     y: source.y,     z: source.z, };  // 2nd method (problem: it pollutes the current scope): let {x, y, z} = source, coords2 = {x, y, z};  // 3rd method (quite hard to read for such simple task): let coords3 = {}; for (let attr of ['x','y','z']) coords3[attr] = source[attr];  // Similar to the 3rd method, using a function: function extract(src, ...props) {     let obj = {};     props.map(prop => obj[prop] = src[prop]);     return obj; } let coords4 = extract(source, 'x', 'y', 'z'); 

 


One way to do it is through object destructuring and an arrow function:

let source = {     x: 120,     y: 200,     z: 150,     radius: 10,     color: 'red', };  let result = (({ x, y, z }) => ({ x, y, z }))(source);  console.log(result);

The way this works is that the arrow function (({ x, y, z }) => ({ x, y, z })) is immediately called with source as the parameter. It destructures source into x, y, and z, and then immediately returns those as a new object.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: