Skip to content

SafaSelim/ts-flatten-array

Repository files navigation

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"];

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published