我有两份名单
List A have 3 items
List A={[name: abc, desc: abcd, img: httpsURL1],
[name: xyz, desc: wxyz, img: httpsURL2],
[name: def, desc: sahdw, img: httpsURL3]}
List B has only one item and name argument is same in both
List B={[name: abc, progress: 0.75]},现在,我想生成第三个列表,如下所示:
List C = {[name: abc, desc: abcd, img: httpsURL1, progress:0.75],
[name: xyz, desc: wxyz, img: httpsURL2, progress: 0],
[name: def, desc: sahdw, img: httpsURL3, progress: 0]}在达特有可能吗?
发布于 2020-11-23 11:56:52
我在这里做了一个假设,第一个列表总是包含所有的元素。否则,应该以您提供的输入为例,并提供您期望的输出。
检查DartPad:https://dartpad.dev/3cf607d4d0284702319e562099e19b1e中的示例
void main() {
var aList = [
new ClassA("abc", "abcd", "httpsURL1"),
new ClassA("xyz", "wxyz", "httpsURL2"),
new ClassA("def", "sahdw", "httpsURL3")
];
var bList = [
new ClassB("abc", 0.75),
];
var result = aList
.map((a) => bList.any((b) => b.name == a.name)
? new ClassC(a.name, a.desc, a.img,
bList.firstWhere((b) => a.name == b.name).progress)
: new ClassC(a.name, a.desc, a.img, 0))
.toList();
print(result);
}
class ClassA {
String name;
String desc;
String img;
ClassA(this.name, this.desc, this.img);
}
class ClassB {
String name;
double progress;
ClassB(this.name, this.progress);
}
class ClassC {
String name;
String desc;
String img;
double progress;
ClassC(this.name, this.desc, this.img, this.progress);
}https://stackoverflow.com/questions/64967519
复制相似问题