Is it possible to destructure onto an existing obj

2019-01-02 18:03发布

For example if I have two objects:

var foo = {
  x: "bar",
  y: "baz"
}

and

var oof = {}

and I wanted to transfer the x and y values from foo to oof. Is there a way to do that using the es6 destructuring syntax?

perhaps something like:

oof{x,y} = foo

14条回答
闭嘴吧你
2楼-- · 2019-01-02 18:29

You can return the destructured object in an arrow function, and use Object.assign() to assign it to a variable.

const foo = {
  x: "bar",
  y: "baz"
}

const oof = Object.assign({}, () => ({ x, y } = foo));
查看更多
步步皆殇っ
3楼-- · 2019-01-02 18:35

You can just use restructuring for that like this:

const foo = {x:"a", y:"b"};
const {...oof} = foo; // {x:"a", y:"b"} 

Or merge both objects if oof has values:

const foo = {x:"a", y:"b"};
let oof = {z:"c"}
oof = Object.assign({}, oof, foo)
查看更多
爱死公子算了
4楼-- · 2019-01-02 18:37

No, destructuring does not support member expressions in shorthands but only plain propertynames at the current time. There have been talks about such on esdiscuss, but no proposals will make it into ES6.

You might be able to use Object.assign however - if you don't need all own properties, you still can do

var foo = …,
    oof = {};
{
    let {x, y} = foo;
    Object.assign(oof, {x, y})
}
查看更多
只靠听说
5楼-- · 2019-01-02 18:38

I came up with this method:

exports.pick = function pick(src, props, dest={}) {
    return Object.keys(props).reduce((d,p) => {
        if(typeof props[p] === 'string') {
            d[props[p]] = src[p];
        } else if(props[p]) {
            d[p] = src[p];
        }
        return d;
    },dest);
};

Which you can use like this:

let cbEvents = util.pick(this.props.events, {onFocus:1,onBlur:1,onCheck:'onChange'});
let wrapEvents = util.pick(this.props.events, {onMouseEnter:1,onMouseLeave:1});

i.e., you can pick which properties you want out and put them into a new object. Unlike _.pick you can also rename them at the same time.

If you want to copy the props onto an existing object, just set the dest arg.

查看更多
ら面具成の殇う
6楼-- · 2019-01-02 18:42

DRY

var a = {a1:1, a2: 2, a3: 3};
var b = {b1:1, b2: 2, b3: 3};

const newVar = (() => ({a1, a2, b1, b2})).bind({...a, ...b});
const val = newVar();
console.log({...val});
// print: Object { a1: 1, a2: 2, b1: 1, b2: 2 }

or

console.log({...(() => ({a1, a2, b1, b2})).bind({...a, ...b})()});
查看更多
姐姐魅力值爆表
7楼-- · 2019-01-02 18:47

IMO this is the easiest way to accomplish what you're looking for:

let { prop1, prop2, prop3 } = someObject;
let data = { prop1, prop2, prop3 };

  // data === { prop1: someObject.prop1, ... }

Basically, destructure into variables and then use the initializer shorthand to make a new object. No need for Object.assign

I think this is the most readable way, anyways. You can hereby select the exact props out of someObject that you want. If you have an existing object you just want to merge the props into, do something like this:

let { prop1, prop2, prop3 } = someObject;
let data = Object.assign(otherObject, { prop1, prop2, prop3 });
    // Makes a new copy, or...
Object.assign(otherObject, { prop1, prop2, prop3 });
    // Merges into otherObject

Another, arguably cleaner, way to write it is:

let { prop1, prop2, prop3 } = someObject;
let newObject = { prop1, prop2, prop3 };

// Merges your selected props into otherObject
Object.assign(otherObject, newObject);

I use this for POST requests a lot where I only need a few pieces of discrete data. But, I agree there should be a one liner for doing this.

查看更多
登录 后发表回答