您当前的位置: 首页 >  ar

大前端之旅

暂无认证

  • 4浏览

    0关注

    403博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

从Dart列表中删除重复项的2种方法

大前端之旅 发布时间:2021-09-08 12:45:26 ,浏览量:4

本文向您展示了从 Flutter 中的列表中删除重复项的 2 种方法。第一个适用于原始数据类型列表。第二个稍微复杂一些,但适用于map****列表或对象列表。

转换为 Set 然后反转为 List

这是一个简单列表的简单快速的解决方案。

例子:

void main(){
  final myNumbers = [1, 2, 3, 3, 4, 5, 1, 1];
  final uniqueNumbers = myNumbers.toSet().toList();
  print(uniqueNumbers);
  
  final myStrings = ['a', 'b', 'c', 'a', 'b', 'a'];
  final uniqueStrings = myStrings.toSet().toList();
  print(uniqueStrings); 
}

输出:

[1, 2, 3, 4, 5]
[a, b, c]
从map或对象列表中删除重复项

我们的策略是将列表的每个项目转换为 JSON 字符串,然后像第一种方法一样使用toSet()和toList()。

例子:

import "dart:convert";
void main(){
  final myList = [
    {
      'name': 'Andy',
      'age': 41
    },
    {
      'name': 'Bill',
      'age': 43
    },
    {
      'name': 'Andy',
      'age': 41
    }
  ];
  
  // convert each item to a string by using JSON encoding
  final jsonList = myList.map((item) => jsonEncode(item)).toList();
  
  // using toSet - toList strategy
  final uniqueJsonList = jsonList.toSet().toList();
  
  // convert each item back to the original form using JSON decoding
  final result = uniqueJsonList.map((item) => jsonDecode(item)).toList();
  
  print(result); 
}

输出:

[{name: Andy, age: 41}, {name: Bill, age: 43}]
关注
打赏
1660524863
查看更多评论
立即登录/注册

微信扫码登录

0.0416s