1.0.0 • Published 2 years ago

ts-flatten-array v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Flatten Array in Typescript

Flattens nested arrays into one single array.

Installation

In your roject, run:

$ npm install ts-flatten-array --save-dev

Include the function:

import { flattenArray } from 'ts-flatten-array';

Basic Usage

Flattens an array without type declration:

flattenArray([1, [2, 3, [4, 5], 6, [7]]]); // => [1, 2, 3, 4, 5, 6, 7]

For Type Safety

Include the type:

import { NestedArray } from 'ts-flatten-array';

let inputArrayNumber: NestedArray<number> = [1, [2, [3, 3, [6, 3, 1, 2, 4]]], 4];
let inputArrayString: NestedArray<string> = ["apple", ["pear", ["cherry", "berry", ]], "grape"];
let inputArrayNumberAndString: NestedArray<string | number> = ["apple", ["pear", ["cherry", [3, 3, [6, 3, 1, 2, 4]], "berry", ]], "grape"];