Maps the provided array into a map

  • Type Parameters

    • T
    • K
    • V

    Parameters

    • arr: T[]

      Array of items to be entered into a map

    • fn: ((item: T, index: number) => [key: K, value: V])

      A mapping function to transform each item into a key value pair

        • (item, index): [key: K, value: V]
        • Parameters

          • item: T
          • index: number

          Returns [key: K, value: V]

    Returns Map<K, V>

    const map = arrayToMap([5, 4, 3, 2, 1], (item, i) => [i, item]);

    console.log(map); // Map(5) { 0 => 5, 1 => 4, 2 => 3, 3 => 2, 4 => 1 }