简体   繁体   中英

Partial object copying using Destructuring Assignment

It is very common to have an input object that you need to copy to another object. Often you'll need to copy some of the properties, and you end up with a very similar object but with less properties.

Reference for Destructuring Assignment here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring

Assume you have this input object:

// an object with data
let oldObject = {
  a: 'this is a',
  b: 'this is b',
  c: 'this is c',
  d: 'this is d'
}

This is how I did it:

let myNewObject = {
  a: oldObject.a,
  b: oldObject.b,
  c: oldObject.c
}

This is the one I've been using:

// new version
let { a, b, c } = oldObject
let myNewObject = { a, b, c };

This one I'm starting to use, seems to work ok:

// newer version
let myNewObject = { a, b, c } = oldObject;

The problem with the newer versions is that I'm declaring variables a, b and c. In this case there is no problem ( almost ), but what if their names are input , output , temp , i or any other common name for variables that might have been already declared in the scope.

So the solution is great, but it would be even better is those variables are somehow created and destroyed in that line and not available elsewhere. myNewObject should be accessible of course.

I tried wrapping it in a function like this, but no luck at all:

let myNewObject = (function() { return { a, b, c } = oldObject })();

let myNewObject = (function() { let temp = { a, b, c } = oldObject; return temp; })();

So I'm not sure what is the best way to achieve this, I'm getting close, but I'm missing something.

The problem with the newer versions is that I'm declaring variables a, b and c. In this case there is no problem

There is a big problem.

let myNewObject = { a, b, c } = oldObject;

assigns to existing a , b and c variables, this will pollute global scope in loose mode or will result in error in strict mode. While myNewObject === oldObject . Its ES5 variation is:

var myNewObject = (a = oldObject.a, b = oldObject.b, c = oldObject.c, oldObject);

As explained in this answer , a one-liner you're looking for is IIFE with destructured parameter:

let myNewObject = (({ a, b, c }) => ({ a, b, c }))(oldObject);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM