Friday, January 30, 2026
HomeLanguagesJavascriptTensorflow.js tf.data.Dataset class .toArray() Method

Tensorflow.js tf.data.Dataset class .toArray() Method

Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .toArray() method is used to accumulate each elements of the stated dataset within an array.

Syntax:  

toArray()

Parameters: This method does not hold any parameter.

Return Value: It returns Promise( T[] ).

Note:

  • This method applies for limited datasets only i.e. the one that fits in the memory.
  • It is utilized for testing purpose and should be mostly abstained when conceivable.

Example 1:  

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining dataset formed of an array
// of numbers
const res = tf.data.array([11, 12, 31, 43, 15, 64]);
  
// Calling toArray() method and
// Printing output
console.log(await res.toArray());


Output:

11, 12, 31, 43, 15, 64

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling toArray() method and
// Printing output
console.log(await tf.data.array([5.7, 
    -16, .31, 0, null, NaN]).toArray());


Output:

5.7, -16, 0.31, 0, , NaN

Reference: https://js.tensorflow.org/api/latest/#tf.data.Dataset.toArray

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12065 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS