Flutter Iterable.reduce

Iterable.reduce 概念

官方解释:

Reduces a collection to a single value by iteratively combining elements
/// of the collection using the provided function.
///
/// The iterable must have at least one element.
/// If it has only one element, that element is returned.
///
/// Otherwise this method starts with the first element from the iterator,
/// and then combines it with the remaining elements in iteration order,
/// as if by:
/// dart /// E value = iterable.first; /// iterable.skip(1).forEach((element) { /// value = combine(value, element); /// }); /// return value; ///
/// Example of calculating the sum of an iterable:
/// ```dart
/// iterable.reduce((value, element) => value + element);

我看了反正是没看懂,实践是检验真理的唯一标准

List<String> testList = ["111","222","333"];
var reduce = testList.reduce((value, element) => value + "=" + element);
print("reduce: $reduce");

输出结果: reduce: 111=222=333

简单来说就是将集合按照指定规则合并为一个字符串,感兴趣的可以自己测试一下


版权声明:本文为S1066430476原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。