问题描述
在某些时候我需要删除一些对象属性,但是我仍然需要其他一些对象属性,因此我不能只写 delete vm.model;
来删除所有对象.
I have a few object properties I need to delete at certain point but I still need others so I can't just write delete vm.model;
to remove all of it.
目前我需要删除5个属性,但是列表很可能会增加,所以我不想结束.
At the moment there are 5 properties I need to delete but the list will most likely grow so I don't want to end up with.
delete vm.model.$$hashKey;
delete vm.model.expiryDate;
delete vm.model.sentDate;
delete vm.model.startDate;
delete vm.model.copied;
我正在寻找一种能为我做的衬板.我做了一些研究,发现_.omit lodash函数,但我不想最终只为单个函数下载整个库.
I'm looking for a one liner that will do it for me. I did some research and found the _.omit lodash function but I don't want to end up downloading whole library just for a single function.
是否有一个angularjs/js函数可以一次清除多个属性?
Is there a angularjs/js function that will clear multiple properties at once?
推荐答案
尚无本机函数可以执行此操作.但您始终可以循环并删除
:
There is no native function that does this yet; but you can always just loop and delete
:
const obj = {
prop1: 'foo',
prop2: 'bar',
prop3: 'baz'
}
;[ 'prop1', 'prop2' ].forEach(prop => {
delete obj[prop]
})
console.log(obj.prop1, obj.prop2, obj.prop3)
只需将上面的循环放到一个可重用的函数中,即 deleteKeys(obj,keys)
.
Just put the above loop into a function which you can reuse, i.e deleteKeys(obj, keys)
.
这篇关于一线删除多个对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!