将一个对象中的某个属性删除,并形成新的一个对象的方法有:
// 假设要删除cc属性,并返回一个新的newObj = { aa: 1, bb: 2, dd: 4, ee: 5 }
var obj = {
aa: 1,
bb: 2,
cc: 3,
dd: 4,
ee: 5
1.for + if
let newObj = {}
for (let key in obj) {
if (key !== 'cc') {
newObj[key] = obj[key];
2 delete
let newObj = JSON.parse(JSON.stringify(obj, ['aa', 'bb', 'cc', 'dd', 'ee']));
delete newObj.cc;
3.JSON.stringify的第二个参数
let newObj = JSON.parse(JSON.stringify(obj, ['aa', 'bb', 'dd', 'ee']));
4.解构赋值
let {aa, bb, dd, ee} = obj;
let newObj = {aa, bb, dd, ee};
5.rest参数
const delKey = (prop, { [prop]: _, ...rest }) => rest;
let newObj = delKey('cc', obj);
其中_可以是任何字符,比如,其值为cc对应的3
6.undifined
obj.cc = undefined;
let newObj = JSON.parse(JSON.stringify(obj));
复制代码