我在Dart中有两个列表,
final List availableIssueComponents = [
{'id': 1, 'componentName': 'Cash Acceptor'},
{'id': 2, 'componentName': 'Printer'},
{'id': 3, 'componentName': 'PIN Pad'},
{'id': 4, 'componentName': 'Key Board'},
{'id': 5, 'componentName': 'Touch Screen'},
{'id': 6, 'componentName': 'Computer'},
{'id': 7, 'componentName': 'Application'},
{'id': 8, 'componentName': 'Network'},
{'id': 9, 'componentName': 'Power'},
{'id': 10, 'componentName': 'Camera'},
{'id': 11, 'componentName': 'Safe'},
{'id': 13, 'componentName': 'Screen'},
{'id': 14, 'componentName': 'Battery'},
{'id': 15, 'componentName': 'Ports'},
{'id': 16, 'componentName': 'Application'},
{'id': 17, 'componentName': 'Safe'},
{'id': 18, 'componentName': 'Camera'},
{'id': 19, 'componentName': 'Power'},
{'id': 20, 'componentName': 'Key Board'},
{'id': 21, 'componentName': 'PIN Pad'},
{'id': 22, 'componentName': 'Printer'},
{'id': 23, 'componentName': 'Computer'},
{'id': 24, 'componentName': 'Touch Screen'},
{'id': 25, 'componentName': 'Application'},
{'id': 26, 'componentName': 'Network'}
];
final List selectedIssueComponents = [
{'id': 3, 'componentName': 'PIN Pad'},
{'id': 6, 'componentName': 'Computer'},
{'id': 19, 'componentName': 'Power'},
];
从上面两个列表中,我试图选择availableIssueComponents
中的所有元素,不包括selectedIssueComponents
中已经可用的元素。
示例:由于id为3
,6
,19
的组件在两个列表中都很常见,因此我想要第三个列表包含所有组件,不包括id为3
,6
,19
的组件。
第三个列表应该如下所示,
final List availableIssueComponents = [
{'id': 1, 'componentName': 'Cash Acceptor'},
{'id': 2, 'componentName': 'Printer'},
{'id': 4, 'componentName': 'Key Board'},
{'id': 5, 'componentName': 'Touch Screen'},
{'id': 7, 'componentName': 'Application'},
{'id': 8, 'componentName': 'Network'},
{'id': 9, 'componentName': 'Power'},
{'id': 10, 'componentName': 'Camera'},
{'id': 11, 'componentName': 'Safe'},
{'id': 13, 'componentName': 'Screen'},
{'id': 14, 'componentName': 'Battery'},
{'id': 15, 'componentName': 'Ports'},
{'id': 16, 'componentName': 'Application'},
{'id': 17, 'componentName': 'Safe'},
{'id': 18, 'componentName': 'Camera'},
{'id': 20, 'componentName': 'Key Board'},
{'id': 21, 'componentName': 'PIN Pad'},
{'id': 22, 'componentName': 'Printer'},
{'id': 23, 'componentName': 'Computer'},
{'id': 24, 'componentName': 'Touch Screen'},
{'id': 25, 'componentName': 'Application'},
{'id': 26, 'componentName': 'Network'}
];
我试着用set来做这个,下面是我的方法,
Set availableComponentsSet = Set.from(availableIssueComponents);
Set issueComponentsSet = Set.from(selectedIssueComponents);
Set resultComponents = availableComponentsSet.difference(issueComponentsSet);
但是当登录到控制台时,它的resultComponents
包含了所有的组件。这不是我想要的。我也试过嵌套的for循环,它也不起作用。
组件对象在使用Set<Map>
时不会被Set
过滤,因为Map
是一个引用类型,并且被认为是唯一的,除非被比较的两个对象指向相同的实例(如@Pat9RB注释)。
我会将选中的id映射到一个列表,然后使用List#where(fn)
过滤掉这些id
final availableIssueComponents = [
{'id': 1, 'componentName': 'Cash Acceptor'},
{'id': 2, 'componentName': 'Printer'},
{'id': 3, 'componentName': 'PIN Pad'},
{'id': 4, 'componentName': 'Key Board'},
{'id': 5, 'componentName': 'Touch Screen'},
{'id': 6, 'componentName': 'Computer'},
{'id': 7, 'componentName': 'Application'},
{'id': 8, 'componentName': 'Network'},
{'id': 9, 'componentName': 'Power'},
{'id': 10, 'componentName': 'Camera'},
{'id': 11, 'componentName': 'Safe'},
{'id': 13, 'componentName': 'Screen'},
{'id': 14, 'componentName': 'Battery'},
{'id': 15, 'componentName': 'Ports'},
{'id': 16, 'componentName': 'Application'},
{'id': 17, 'componentName': 'Safe'},
{'id': 18, 'componentName': 'Camera'},
{'id': 19, 'componentName': 'Power'},
{'id': 20, 'componentName': 'Key Board'},
{'id': 21, 'componentName': 'PIN Pad'},
{'id': 22, 'componentName': 'Printer'},
{'id': 23, 'componentName': 'Computer'},
{'id': 24, 'componentName': 'Touch Screen'},
{'id': 25, 'componentName': 'Application'},
{'id': 26, 'componentName': 'Network'}
];
final selectedIssueComponents = [
{'id': 3, 'componentName': 'PIN Pad'},
{'id': 6, 'componentName': 'Computer'},
{'id': 19, 'componentName': 'Power'},
];
final selectedIds = selectedIssueComponents.map((component) => component['id']).toList();
final filtered = availableIssueComponents.where((element) => !selectedIds.contains(element["id"])).toList();
print(filtered);
如果你喜欢使用Set
和difference
,你可以创建一组id。这将创建一组int
(Set<int>
),这是一个基本类型,并允许预期的过滤类型:
final availableIssueComponents = [
{'id': 1, 'componentName': 'Cash Acceptor'},
{'id': 2, 'componentName': 'Printer'},
{'id': 3, 'componentName': 'PIN Pad'},
{'id': 4, 'componentName': 'Key Board'},
{'id': 5, 'componentName': 'Touch Screen'},
{'id': 6, 'componentName': 'Computer'},
{'id': 7, 'componentName': 'Application'},
{'id': 8, 'componentName': 'Network'},
{'id': 9, 'componentName': 'Power'},
{'id': 10, 'componentName': 'Camera'},
{'id': 11, 'componentName': 'Safe'},
{'id': 13, 'componentName': 'Screen'},
{'id': 14, 'componentName': 'Battery'},
{'id': 15, 'componentName': 'Ports'},
{'id': 16, 'componentName': 'Application'},
{'id': 17, 'componentName': 'Safe'},
{'id': 18, 'componentName': 'Camera'},
{'id': 19, 'componentName': 'Power'},
{'id': 20, 'componentName': 'Key Board'},
{'id': 21, 'componentName': 'PIN Pad'},
{'id': 22, 'componentName': 'Printer'},
{'id': 23, 'componentName': 'Computer'},
{'id': 24, 'componentName': 'Touch Screen'},
{'id': 25, 'componentName': 'Application'},
{'id': 26, 'componentName': 'Network'}
];
final selectedIssueComponents = [
{'id': 3, 'componentName': 'PIN Pad'},
{'id': 6, 'componentName': 'Computer'},
{'id': 19, 'componentName': 'Power'},
];
final availableIds = availableIssueComponents.map((component) => component['id']).toSet();
final selectedIds = selectedIssueComponents.map((component) => component['id']).toSet();
final filteredIds = availableIds.difference(selectedIds);
final filteredComponents = availableIssueComponents.where((element) => filteredIds.contains(element["id"])).toList();
print(filteredComponents);