简体   繁体   English

如何比较两个动态列表以识别匹配和不匹配的记录,使用id(这将是唯一的)

[英]How to compare Two List of dynamic to identify the matching and non matching records, using the id (which will be unique)

Requirement is to get all the matching and non matching records from the List of Map in dart.要求是从 dart 的 Map 列表中获取所有匹配和不匹配的记录。

 var a = [
{"id":"1","b":"2"},
{"id":"2","d":"4"},
{"id":"3","f":"6"}
]; 

 var b = [
{"id":"1","b":"2"},
{"id":"3","d":"6"}
];

How do I obtain a difference of:我如何获得以下差异:

c = [{"id":"2","d":"4"}]

I am not sure if I've understood it well, but this would be my first approach.我不确定我是否理解得很好,但这将是我的第一种方法。 Time complexity isn't the best, it probably can be optimized.时间复杂度不是最好的,它可能可以优化。 I may try to do it when I'll be sure it is what you're looking for.当我确定它是您正在寻找的东西时,我可能会尝试这样做。

typedef mapList = List<Map<String, String>>;

mapList substractMaps(mapList a, mapList b) {
  mapList difference = [];
  a.forEach((aMap){
    final currentIdValue = aMap['id'];
    bool found = false;
    b.forEach((bMap) {
      if(bMap['id'] == currentIdValue) {
        found = true;
      }
    });
    if(!found) {
      difference.add(aMap);
    }
  });
    
 return difference;
}

you can compare as below:您可以进行如下比较:

for non-matching record as:对于不匹配的记录为:

  var c= a.where((e)=>b.where((ee)=>e['id']==ee['id']).toList().isEmpty).toList();

for matching record as:匹配记录为:

 var c= a.where((e)=>b.where((ee)=>e['id']==ee['id']).toList().isNotEmpty).toList();

You have two lists of maps that you want to take the difference of.您有两个要从中获取差异的地图列表。 The quickest solution is to turn the two lists into Sets of immutable Maps which can be properly used as Set keys, then take the Set difference.最快的解决方案是将这两个列表转换为可以正确用作 Set 键的不可变 Map 的 Sets,然后取 Set 差异。 Like this:像这样:

import 'package:fast_immutable_collections/fast_immutable_collections.dart';

void main(List<String> arguments) {
  final a = [
    {"id": "1", "b": "2"},
    {"id": "2", "d": "4"},
    {"id": "3", "f": "6"}
  ];

  final b = [
    {"id": "1", "b": "2"},
    {"id": "3", "d": "6"}
  ];

  final aSet = a
      .map(
        (e) => IMap(e),
      )
      .toSet();
  final bSet = b
      .map(
        (e) => IMap(e),
      )
      .toSet();

  print(
    aSet.difference(bSet).toList(),
  );
}

This results in:这导致:

[{
   id: 2,
   d: 4
}, {
   id: 3,
   f: 6
}]

Which seems to be what you wanted.这似乎是你想要的。

Update: not quite.更新:不完全。 You didn't specify what happens with {"id":"3","d":"6"} .您没有指定{"id":"3","d":"6"}会发生什么。 I include it as a difference of these sets.我把它作为这些集合的区别。 You'll need to clarify your problem.你需要澄清你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 flutter 方法中的包含比较两个列表的元素并找到匹配的元素? - How to compare elements of two lists and find matching ones using the contains in flutter method? 应该使用 List 的哪种方法将两个匹配的单词分配给 value - Which method of List should be used to assign two matching words to the value Flutter:如何使用匹配的 id 和索引从两个集合中读取来自 firestore 的数据 - Flutter : How to read data from firestore from two collections using matching id and index dart中使用动态全词匹配正则表达式过滤列表 - Filter list using dynamic whole word matching regex in dart Dart/Flutter - 比较两个列表<dynamic>如果它们具有相同的 id 值 - Dart/Flutter - Compare two List<dynamic> if they have the same value of id 两个匹配某个条件的列表中的多个对象如何求和 - How two sum multiple objects in a list matching a certain condition 如何在 flutter 的两个地图中找到匹配值? - How to find matching values in two maps in flutter? 如何比较 2 个不同长度的 arrays 并寻找匹配值 - Flutter - How to compare 2 arrays of different lengths and look for matching values - Flutter NoSuchMethodError: Class '列表<dynamic> ' 没有匹配 arguments 的实例方法 'cast'</dynamic> - NoSuchMethodError: Class 'List<dynamic>' has no instance method 'cast' with matching arguments 查询匹配列表未显示 - Query Matching list is not displaying
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM