Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I filter a map / access the entries

Tags:

The Map interface doesn't seem to provide access to the entries as an iterable, nor does it expose a where method to filter entries. Am I missing something? Is there a simple workaround?

e.g.

Map map; final filteredMap = map.where((k, v) => k.startsWith("foo")); 
like image 739
Anders Avatar asked Jan 15 '14 06:01

Anders


1 Answers

Dart 2.0.0 added removeWhere which can be used to filter Map entities. Given your example, you could apply this as:

Map map; final filteredMap = Map.from(map)..removeWhere((k, v) => !k.startsWith("foo")); 

It's not the where method you asked for, but filtering Map entities is certainly doable this way.

like image 87
András Szepesházi Avatar answered Sep 21 '22 20:09

András Szepesházi