过滤掉对象成员的好习惯(javascript)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8779304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Good idiom to filter out members of an object (javascript)
提问by Grumdrig
I'd like to remove certain members of an object (for the sake of argument, those whose keys start with '_'). What's an elegant way to do this? The na?ve way would be:
我想删除对象的某些成员(为了论证,那些键以“_”开头的成员)。什么是优雅的方式来做到这一点?天真的方法是:
for (var i in obj)
if (i[0] === '_')
delete obj[i];
but that modifies the underlying object during the iteration. In Node at least I guess I could
但这会在迭代期间修改底层对象。至少在 Node 中,我想我可以
Object.keys(obj).forEach(function (i) { if (i[0] === '_') delete obj[i]; });
or restart the iteration each time something's deleted with an awkward nested loop.
或者每次使用尴尬的嵌套循环删除某些内容时重新启动迭代。
Are there any better solutions?
有没有更好的解决方案?
EDIT: In testing just now, in node.js at least, the na?ve solution actually seems to work. It certainly is possible that for...in is (required to be) implemented safely. Anyone know?
编辑:在刚才的测试中,至少在 node.js 中,天真的解决方案实际上似乎有效。for...in 肯定有可能(需要)安全地实现。有人知道吗?
回答by OnTheFly
You do not need to worry about it. An excerpt for ECMAScript Language Specification§12.6.4 explicitly states (emphasised by me):
您无需担心。ECMAScript 语言规范§12.6.4的摘录明确指出(由我强调):
The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified. Properties of the object being enumerated may be deleted during enumeration. If a property that has not yet been visited during enumeration is deleted, then it will not be visited.If new properties are added to the object being enumerated during enumeration, the newly added properties are not guaranteed to be visited in the active enumeration. A property name must not be visited more than once in any enumeration.
枚举属性的机制和顺序(第一个算法中的步骤 6.a,第二个算法中的步骤 7.a)没有指定。 被枚举对象的属性可能会在枚举过程中被删除。如果在枚举期间尚未访问的属性被删除,则不会访问它。如果在枚举期间向被枚举的对象添加了新属性,则不能保证在活动枚举中访问新添加的属性。在任何枚举中,不能多次访问属性名称。
回答by Dan D.
why not create a list of the names to remove like
为什么不创建一个要删除的名称列表,例如
var l = [];
for (var i in obj)
if (i[0] === '_')
l.push(i);
l.forEach(function(v){ delete obj[v]; });
回答by Esailija
Object.keys(obj).filter(function (v) {
return v[0] === "_";
}).forEach(function (v) {
delete obj[v];
});
This will modify the object during a loop though ;p
这将在循环期间修改对象;p
If you use this more than once the generic is:
如果您多次使用它,则泛型是:
Object.keys(obj).filter(function (v) {
//filter the object values/keys by some conditions
}).forEach( del.bind(obj) );
function del (v) {
delete this[v];
}
回答by Miquel
An alternative is to create a function returning a filtered object. I would prefer this solution in order to avoid collateral effects on other parts of the code holding a reference to the object being modified.
另一种方法是创建一个返回过滤对象的函数。我更喜欢这个解决方案,以避免对持有对被修改对象的引用的代码的其他部分产生附带影响。
function filterObject(obj) {
var filtered = new Object();
for (var i in obj)
if (i[0] != '_')
filtered[i] = obj[i];
return filtered;
}