问题
What is the Dart idiomatic way to remove selected keys from a Map? Below I'm using a temporary emptyList to hold String keys. Is there a cleaner way?
List<String> emptyList = new List<String>();
_objTable.keys.forEach((String name) {
if (_objTable[name].indices.isEmpty) {
emptyList.add(name);
print("OBJ: deleting empty object=$name loaded from url=$url");
}
});
emptyList.forEach((String name) => _objTable.remove(name));
回答1:
You can do something like this :
_objTable.keys
.where((k) => _objTable[k].indices.isEmpty) // filter keys
.toList() // create a copy to avoid concurrent modifications
.forEach(_objTable.remove); // remove selected keys
回答2:
Simply do:
_objTable.removeWhere((key, value) => key == "propertyName");
It works with nested Maps too.
来源:https://stackoverflow.com/questions/17038512/delete-selected-keys-from-dart-map