ts-prime
Version:
A utility library for JavaScript and Typescript.
46 lines • 1.78 kB
TypeScript
/**
* Map each element of an array into an object using a defined callback function and flatten the result.
* @param array The array to map.
* @param fn The mapping function, which should return an Array of key-value pairs, similar to Object.fromEntries
* @returns The new mapped object.
* @signature
* P.flatMapToObj(array, fn)
* P.flatMapToObj.indexed(array, fn)
* @example
* P.flatMapToObj([1, 2, 3], (x) =>
* x % 2 === 1 ? [[String(x), x]] : []
* ) // => {1: 1, 3: 3}
* P.flatMapToObj.indexed(['a', 'b'], (x, i) => [
* [x, i],
* [x + x, i + i],
* ]) // => {a: 0, aa: 0, b: 1, bb: 2}
* @data_first
* @indexed
* @category Array
*/
export declare function flatMapToObj<T, K extends string | number | symbol, V>(array: readonly T[], fn: (element: T, index: number, array: readonly T[]) => [K, V][]): Record<K, V>;
/**
* Map each element of an array into an object using a defined callback function and flatten the result.
* @param fn The mapping function, which should return an Array of key-value pairs, similar to Object.fromEntries
* @returns The new mapped object.
* @signature
* P.flatMapToObj(fn)(array)
* P.flatMapToObj(fn)(array)
* @example
* P.pipe(
* [1, 2, 3],
* P.flatMapToObj(x => (x % 2 === 1 ? [[String(x), x]] : []))
* ) // => {1: 1, 3: 3}
* P.pipe(
* ['a', 'b'],
* P.flatMapToObj.indexed((x, i) => [
* [x, i],
* [x + x, i + i],
* ])
* ) // => {a: 0, aa: 0, b: 1, bb: 2}
* @data_last
* @indexed
* @category Array
*/
export declare function flatMapToObj<T, K extends string | number | symbol, V>(fn: (element: T, index: number, array: readonly T[]) => [K, V][]): (array: readonly T[]) => Record<K, V>;
//# sourceMappingURL=flatMapToObj.d.ts.map