【问题标题】:Compare elements in two lists and make a new List in Dart比较两个列表中的元素并在 Dart 中创建一个新列表
【发布时间】:2023-03-27 00:20:02
【问题描述】:

我有两个列表,我想通过比较两个列表来创建一个新列表,新列表应该只包含每个列表中包含的元素。

List1=[[id: 1, label: 'cocoa'],[id: 2, label: 'apple'],[id: 3, label: 'cherry'],[id: 4, label: 'banana'],[id: 5, label: 'melon'],[id: 6, label: 'orange'],[id: 7, label: 'pineapple'],[id: 8], label: 'strawberry']
List2=[2,5,7,8]
expectedList = [[id: 2, label: 'apple'],[id: 5, label: 'melon'],[id: 7, label: 'pineapple'],[id: 8], label: 'strawberry']]

下面是真正的代码:

"options": [
                    {
                        "id": 58,
                        "label": "cocoa",
                        "swatch_value": null,
                        "products": [
                            118
                        ]
                    },
                    {
                        "id": 59,
                        "label": "dark chocolate",
                        "swatch_value": null,
                        "products": [
                            120,
                            127
                        ]
                    },
                    {
                        "id": 60,
                        "label": "apple",
                        "swatch_value": null,
                        "products": [
                            121,
                            128
                        ]
                    },
                    {
                        "id": 61,
                        "label": "milk",
                        "swatch_value": null,
                        "products": [
                            122
                        ]
                    },
                    {
                        "id": 62,
                        "label": "coconut",
                        "swatch_value": null,
                        "products": [
                            130
                        ]
                    },
                    {
                        "id": 65,
                        "label": "cherry",
                        "swatch_value": null,
                        "products": [
                            126
                        ]
                    }
                ]

所以第一个列表将包含上面的 json,第二个列表包含下面等于某些 ID 的数字。

List<int> secondList = [58, 59, 60, 61];

现在,当我想通过将 secondList 与第一个列表的 ID 进行比较来生成一个新列表时,第三个列表是空的。

List thirdList = firstList.options.where((element) => secondList.contains(element.id)).toList();

【问题讨论】:

    标签: flutter dart


    【解决方案1】:
      var list1 = [1,2,3,4,5,6,7,8];
      var list2 = [2,5,7,8];
      var expectedList = list1.toSet().intersection(list2.toSet()).toList();
    
      print(expectedList.toString());
    

    更多信息here

    尊重您的信息:

      var dataList = data['options'];
      var firstList = List<int>.generate(dataList.length, (i) => dataList[i]['id']);
      var secondList = [58, 59, 60, 61];
      var thirdList = firstList.toSet().intersection(secondList.toSet()).toList();
      var filtered = dataList.where((e) => thirdList.contains(e['id'])).toList();
      print(filtered.toString());
    

    地点:

    const data = {
      "options": [
    ...
      ]
    };
    

    结果是:

    [{id: 58, label: cocoa, swatch_value: null, products: [118]}, {id: 59, label: dark chocolate, swatch_value: null, products: [120, 127]}, {id: 60, label: apple, swatch_value: null, products: [121, 128]}, {id: 61, label: milk, swatch_value: null, products: [122]}]
    

    【讨论】:

    • 感谢您的帮助,如果您检查我的代码,我不会将两个列表直接相互比较,而是将第二个列表与第一个列表的 ID 进行比较。所以下面基于你的答案的代码仍然是空的。 thirdList = firstList.options .toSet() .intersection(secondList.toSet()) .toList();
    • 感谢 Gain,现在列表只返回 id,但我想要 json 中的 id 列表。所以基本上我想过滤选项列表以仅包含其 ID 在 secondList 中的那些列表。不仅仅是身份证。再次感谢它非常有帮助,但不是我想要的
    • 更新我的问题
    • 谢谢老哥,我知道了,我还要多学习。谢谢
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 2019-12-07
    • 2016-11-17
    • 2020-07-04
    • 2015-11-06
    • 2014-01-07
    相关资源
    最近更新 更多