从Dart Map删除选定的关键点 [英] Delete selected keys from Dart Map

查看:1126
本文介绍了从Dart Map删除选定的关键点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从地图上删除选定键的Dart惯用方式是什么? 下面,我使用一个临时的emptyList来保存字符串键. 有没有更清洁的方法?

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));

推荐答案

您可以执行以下操作:

_objTable.keys
  .where((k) => _objTable[k].indices.isEmpty) // filter keys
  .toList() // create a copy to avoid concurrent modifications
  .forEach(_objTable.remove); // remove selected keys

这篇关于从Dart Map删除选定的关键点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆