![](/img/trans.png)
[英]How to use “reduce” to get the sum of values in a list of objects in Dart/Flutter
[英]how to get the values not repeated in the list? (Dart - Flutter)
我有两个清单:
a = [1,2,3,4,5]
b = [1,4,5]
我想从列表a
中获取列表b
不存在的值:
result = [2,3]
有多种方法可以实现这一点,哪种方法适合您的情况取决于您的要求(问题中未指定)。
您是否要在a
保留重复项:
a = [1,2,2,3,4,5]
b = [1,4,5]
这应该导致[2,3]
还是[2,2,3]
?
如果不想保留重复项,可以将列表转换为Set
并使用difference
方法:
result = a.toSet().difference(b.toSet())
如果您想保留重复项,只需filter
列表即可。
result = a.filter((v) => b.contains(v))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.