浅拷贝对象遗漏ES6/ES7中的一个或多个属性?

Eva*_*bbs 5 javascript ecmascript-harmony ecmascript-6 babeljs ecmascript-7

这就是我一直在做的事情:

var props = { id: 1, name: 'test', children: [] }

//copy props but leave children out
var newProps = { ...props }
delete newProps.children

console.log(newProps) // { id: 1, name: 'test' }
Run Code Online (Sandbox Code Playgroud)

有更清洁,更简单的方法吗?

Ber*_*rgi 9

您可以使用解构分配:

var props = { id: 1, name: 'test', children: [] }

var {children:_, ...newProps} = props;
console.log(newProps) // { id: 1, name: 'test' }
console.log(_) // [] - as an "empty" placeholder
Run Code Online (Sandbox Code Playgroud)

(使用与您已经使用的ES7相同的休息/传播属性提议)