如何使用Lambda过滤地图? [英] How to filter a map using Lambda?
问题描述
我有一个地图变量:
var bitmapDepths:Map<BitmapData, Int>;
我需要删除所有值为0的键,我对此进行了尝试:
What I need is to remove all keys with value of 0, I tried this:
bitmapDepths= Lambda.filter(Lambda.list(bitmapDepths.keys), function(v) { return (v > 0); });
因此,我使用Lambda.list遍历Lambda内的bitmapDepths.keys,但出现此错误:
So, I used Lambda.list to iterate on bitmapDepths.keys inside Lambda, but I get this error:
Void -> Iterator<flash.display.BitmapData> should be Iterable<Unknown<0>>
我尝试了Lambda.array来对bitmapDepths.keys进行迭代,但是我遇到了同样的错误,那么谁可以处理呢?使用Lambda删除基于值的键?
I tried Lambda.array to iterate on bitmapDepths.keys, I got the same error, so who can handle this? to remove keys based on values using Lambda?
推荐答案
请勿使用Lambda.这是Haxe 1中添加的一类.在Haxe 3中,对于循环/理解几乎总是更好的选择.
Don't use Lambda. It's a class that has been added in Haxe 1. In Haxe 3, for loops/comprehensions are almost always the better choice.
要删除适当的键,请执行以下操作:
To remove the keys in place:
for (k in bitmapDepths.keys()) if (k == 0) bitmapDepths.remove(k);
要构建新地图:
bitmapDepths = [for (k in bitmapDepths.keys()) if (k != 0) k => bitmapDepths.get(k)];
它不仅更短,而且具有更好的运行时性能.
Not only is it shorter, it also has better runtime performance.
这篇关于如何使用Lambda过滤地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!