Open In App

TypeScript Array.from() Method

TypeScript `Array.from()` method is a versatile method that transforms array-like or iterable objects into actual arrays. It allows to create arrays from objects with a length property or iterable structures, simplifying the conversion process.

This method is particularly useful for working with collections of data where array operations are needed, providing a convenient and concise way to achieve array representation in TypeScript.

Syntax:

Array.from(object, mapFunction, thisValue)

Parameters:

Return Value:

It returns an array object or instance of an array.

Example 1: In this example, we will be using the Array from() method to create an array from the string value.

// Creating array from input string
let myArr: string[] = Array
    .from("GeeksForGeeks");

// Display output
console.log(myArr);

Output:

[
'G', 'e', 'e', 'k',
's', 'F', 'o', 'r',
'G', 'e', 'e', 'k',
's'
]

Example 2: In this example, we will be using Array from() method to create an array from the string value.

// Creating array form the given string
let myArr: string[] = Array
    .from("45878965412365");

// Display output
console.log(myArr);

Output:

[
'4', '5', '8', '7',
'8', '9', '6', '5',
'4', '1', '2', '3',
'6', '5'
]
Article Tags :