associateWithTo<V> method

Map<E, V> associateWithTo<V>(
  1. Map<E, V> destination,
  2. V valueSelector(
    1. E element
    )
)

生成一个Map映射,其中每个 element 都与 valueSelector 函数生成值相关联,然后追加到 destination中并返回。

举例:

final map = {0: 0};
[1, 2, 3].associateWith((e) => e * 1000); // {0: 0, 1: 1000, 2: 2000, 3: 3000}

Implementation

Map<E, V> associateWithTo<V>(Map<E, V> destination, V Function(E element) valueSelector) {
  final map = <E, V>{};
  forEach((element) {
    map[element] = valueSelector(element);
  });
  return map;
}