我想删除数组中符合的项目并产生不同的数组
var arr = [
{"name": "Peter"},
{"name": "Hana"},
{"name": "Jone"},
{"name": "Peter"},
{"name": "Peter"},
];
结果:
var arr = [
{"name": "Peter"},
{"name": "Hana"},
{"name": "Jone"},
];
答案 0 :(得分:1)
您在这里:
arr = arr.asMap().map((i, m)=> MapEntry<String, Map>(m["name"], m)).values.toList()
答案 1 :(得分:0)
arr = arr.toSet().toList()
检查toSet()
docs
答案 2 :(得分:0)
您可以创建自己的类来解析数据,然后可以比较对象并过滤列表。
您的人物课程
class Person {
final String name;
Person({this.name});
factory Person.from(dynamic json) => Person(name: json["name"]);
Map<String, dynamic> toJson() => {"name": name};
@override
bool operator ==(Object other) =>
identical(this, other) || other is Person && name == other.name;
String toString() => "name : $name";
}
用法
var arr = [
{"name": "Peter"},
{"name": "Hana"},
{"name": "Jone"},
{"name": "Peter"},
{"name": "Peter"},
];
final objectList = arr.map((json) => Person.from(json)).toList();
var filteredList = [];
objectList.forEach(
(item) {
if (!(filteredList.contains(item))) {
filteredList.add(item);
}
},
);
print(filteredList);
答案 3 :(得分:-1)
import 'package:queries/collections.dart';
void main() {
var query = Collection(arr).distinct(CustomEqualityComparer(
(a, b) => a['name'] == b['name'], (a) => a.length));
print(query.toList());
}
var arr = [
{"name": "Peter"},
{"name": "Hana"},
{"name": "Jone"},
{"name": "Peter"},
{"name": "Peter"},
];
结果:
[{name: Peter}, {name: Hana}, {name: Jone}]