1

我在 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,的组件在两个列表619都很常见,所以我想要第三个列表,其中包含除 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'}
  ];

我尝试使用 Sets 来做到这一点,以下是我的方法,

Set availableComponentsSet = Set.from(availableIssueComponents);
Set issueComponentsSet = Set.from(selectedIssueComponents);

Set resultComponents = availableComponentsSet.difference(issueComponentsSet);

但是当登录到控制台时,它resultComponents包含所有组件。这不是我想要的。我也尝试了嵌套的 for 循环,但它也不起作用。

4

1 回答 1

2

Set当使用 a 时,组件对象不会被过滤,Set<Map>因为Map是引用类型,并且被认为是唯一的,除非被比较的两个对象指向同一个实例(如 @Pat9RB 评论的那样)。

我会将选定的 ID 映射到一个列表,然后使用过滤掉这些 IDList#where(fn)

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);
于 2021-09-29T16:00:27.303 回答