最简单的方法是使用 delete
关键字,这样会修改原始数据,并且在不存在这个 key 的时候不会报错:
const foo = {bar: 'baz'}
delete foo.bar //=> ok
delete foo.baz //=> ok
console.log(foo) //=> {}
也可以用 lodash.omit
来返回一个新的 Object
,并支持同时移除多个 key:
const foo = {a: 1, b: 2, c: 3}
_.omit(foo, ['a', 'c']) //=> {b: 2}
_.omit(foo, 'b') //=> {a: 1, c: 3}
还有种情况,就是你同时需要 JSON.stringify(obj)
输出你的内容的时候,你可以直接将该 key 的值设置为 undefined
:
const foo = {bar: 'baz'}
foo.bar = undefined
console.log(JSON.stringify(foo)) //=> '{}'
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too