有没有办法在两个列表之间查找唯一值而无需在dart中使用循环 [英] Is there any way to find unique values between two lists without using a loop in dart
本文介绍了有没有办法在两个列表之间查找唯一值而无需在dart中使用循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在不使用循环的情况下找到两个列表之间的唯一值?
Is there any way to find unique values between two lists without using a loop?
List<String> first = ['A','B','C','D'];
List<String> second = ['B','D'];
我需要这样的结果:
result = ['A','C'];
推荐答案
您可以使用 where( )
和 List
中的 contains()
方法:
You can use where()
with contains()
methods from List
:
void main() {
List<String> first = ['A','B','C','D'];
List<String> second = ['B','D'];
List<String> result = first.where((item) => !second.contains(item)).toList();
print(result); // [A, C]
}
在 DartPad 。
这篇关于有没有办法在两个列表之间查找唯一值而无需在dart中使用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文