CyrefJS
CCyrefJS
فا
Back to documentation
Array/Since 1.38.0

flatten

Combines the elements of nested arrays into a single array.

flatten(array):T[]

Parameters

arrayT[][]required

The array containing nested arrays to flatten.

Returns

T[]

A new array containing all elements from the nested arrays.

Examples

01

Basic usage

Combine multiple nested arrays into a single array.

TypeScript
import { flatten } from "@cyref/js";

const numbers = [
  [1, 2],
  [3, 4],
  [5, 6],
];

const result = flatten(numbers);

console.log(result);
// [1, 2, 3, 4, 5, 6]
02

With strings

Flatten an array containing nested arrays of strings.

TypeScript
import { flatten } from "@cyref/js";

const languages = [
  ["JavaScript", "TypeScript"],
  ["Python", "Go"],
];

const result = flatten(languages);

console.log(result);
// ["JavaScript", "TypeScript", "Python", "Go"]
03

Empty nested arrays

Empty nested arrays do not add any elements to the result.

TypeScript
import { flatten } from "@cyref/js";

const values = [
  [],
  [1, 2],
  [],
  [3],
];

const result = flatten(values);

console.log(result);
// [1, 2, 3]
Built with@cyref/js·Official documentation