Returns every nth element in a list.
Use List.generate()
to generate a list that fits the number of elements that will be returned, then add every nth
element to it.
代码实现
import 'dart:math';
List<T> everyNth<T>(List<T> lst, int nth) {
return List.generate(
(lst.length / nth).floor(), (i) => lst[(i + 1) * nth - 1]);
}
使用样例
everyNth([1, 2, 3, 4, 5, 6], 2); // [ 2, 4, 6 ]