Javascript-不使用删除操作删除对象键 [英] Javascript - Removing object key not using delete
本文介绍了Javascript-不使用删除操作删除对象键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是重复的问题,但我找不到任何问题.
This might be duplicated question, but I could not find any.
我想删除一个对象的键.
I wanna remove a key of an object.
请先检查我的小提琴.
delete obj.something;
当您检查控制台时,尽管在第一个console.log
之后使用了delete,您仍然可以看到在控制台中打印了相同的对象.
When you check console, you'll be able to see the same object is printed in console in spite of using delete after first console.log
.
为什么会这样?
删除对象键的最佳方法是什么?
And what is the best idea of remove a key of an object?
谢谢.
推荐答案
您可以使用扩展运算符删除密钥.
you can use spread operators to remove keys.
let obj = {a:1, b:2, c:3}
const {a, ...restObj} = obj; // this will make restObj with deleted a key.
在处理不可变类型时,此方法特别有用.这样,您就不会变异原始对象,而仍然会获得具有已删除键的对象
This method is especially useful when dealing with immutable types. This way you would not mutate the original object and still get an object with deleted key
这篇关于Javascript-不使用删除操作删除对象键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文