Open In App

TypeScript Array.from() Method

Last Updated : 14 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • object: This Parameter is wanted to specify the object to transform to an array.
  • mapFunction: This Parameter specifies the map function to call on every item of the array.
  • thisValue: This Parameter specifies the charge to apply as this on the equal time as executing the map function.

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.

Javascript
// 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.

Javascript
// 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'
]

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads