问题
I have a map variable:
var bitmapDepths:Map<BitmapData, Int>;
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); });
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>>
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?
回答1:
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);
To construct a new map:
bitmapDepths = [for (k in bitmapDepths.keys()) if (k != 0) k => bitmapDepths.get(k)];
Not only is it shorter, it also has better runtime performance.
来源:https://stackoverflow.com/questions/20299483/how-to-filter-a-map-using-lambda