复制并且排除

1
2
var clone = Object.assign({}, {a: 1, b: 2, c: 3});
delete clone.b;

这个不完美

1
var clone2 = Object.assign({}, {a: 1, b: 2, c: 3}, {b: undefined});

1
2
3
4
5
6
7
8
9
const x = {a: 1, b: 2, c: 3, z:26};

const objectWithoutKey = (object, key) => {
const {[key]: deletedKey, ...otherKeys} = object;
return otherKeys;
}

console.log(objectWithoutKey(x, 'b')); // {a: 1, c: 3, z:26}
console.log(x); // {a: 1, b: 2, c: 3, z:26};