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

前端 未结 16 1817
暗喜
暗喜 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:00

    This works in chrome 53.0.2785.89

    let foo = {
      x: "bar",
      y: "baz"
    };
    
    let oof = {x, y} = foo;
    
    console.log(`oof: ${JSON.stringify(oof)});
    
    //prints
    oof: {
      "x": "bar",
      "y": "baz"
    }
    

提交回复
热议问题