Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 8x 8x 3x 8x 3x 9x 9x 9x | import { PredIndexedOptional } from './_types';
import { purry } from './purry';
/**
* Map each element of an array into an object using a defined callback function.
* @param array The array to map.
* @param fn The mapping function, which should return a tuple of [key, value], similar to Object.fromEntries
* @returns The new mapped object.
* @signature
* P.mapToObj(array, fn)
* @signature
* P.mapToObj(fn)(array)
* @example
* P.mapToObj([1, 2, 3], x => [String(x), x * 2]) // => {1: 2, 2: 4, 3: 6}
* P.pipe(
* [1, 2, 3],
* P.mapToObj(x => [String(x), x * 2])
* ) // => {1: 2, 2: 4, 3: 6}
* P.pipe(
* [0, 0, 0],
* P.mapToObj.indexed((x, i) => [i, i])
* ) // => {0: 0, 1: 1, 2: 2}
* @category Array, Pipe
*/
export function mapToObj<T, K extends string | number | symbol, V>(
array: readonly T[],
fn: (element: T, index: number, array: readonly T[]) => [K, V]
): Record<K, V>;
export function mapToObj<T, K extends string | number | symbol, V>(
fn: (element: T, index: number, array: readonly T[]) => [K, V]
): (array: readonly T[]) => Record<K, V>;
export function mapToObj() {
return purry(_mapToObj(), arguments);
}
const _mapToObj = () => <T>(
array: any[],
fn: PredIndexedOptional<any, any>
) => {
return array.reduce((result, element, index) => {
const [key, value] = fn(element, index, array);
result[key] = value;
return result;
}, {});
};
|