Is it possible to destructure onto an existing object? (Javascript ES6)

前端 未结 16 1792
暗喜
暗喜 2020-11-22 16:24

For example if I have two objects:

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

and

var oof = {}

and I want

16条回答
  •  无人共我
    2020-11-22 17:13

    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));
    

提交回复
热议问题