简体   繁体   English

Javascript“删除”运算符

[英]Javascript “delete” operator

UPD: UPD:
What I really should use to remove some objects from another object? 我真正应该用来从另一个对象中删除一些对象的东西是什么?
Is data[ 'some_key' ] = undefined good enough? data[ 'some_key' ] = undefined足够好?

ORIGINAL QUESTION: 原始问题:
I want to remove some objects that are stored in another object, some like that: 我想删除一些存储在另一个对象中的对象,例如:

var data = {
    'a': {...},
    'b': {...},
    'c': {...},
    ...
}

Is it true that the use of "delete" operator is not a really good practice? 确实,使用“删除”运算符不是一个很好的做法吗?
When I should and shouldn't use it? 什么时候应该和不应该使用它?

Deleting elements in arrays, creates holes (the length property is not updated) 删除数组中的元素,创建孔(length属性未更新)

> var arr = [ 'a', 'b' ];
> arr.length
2
> delete arr[1]  // does not update length
true
> arr
[ 'a',  ]
> arr.length
2

You can also delete trailing array elements by decreasing an array's length. 您还可以通过减小数组的长度来删除结尾的数组元素。

Delete is the only true way to remove object's properties without any leftovers, but it works ~ 100 times slower, compared to it's "alternative", setting object[key] = undefined. 删除是删除对象属性而没有任何剩余物的唯一正确方法,但是与“替代”相比,设置object [key] = undefined的速度要慢100倍左右。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM