0%

复制并且排除

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};
1
2
3
const obj = { a: 1, b: 2, c: 3, d: 4 }
const clone = (({ b, c, ...o }) => o)(obj) // remove b and c
console.log(clone)

lodash

1
_.omit(x, 'b')