Saturday, August 30, 2025
HomeLanguagesJavascriptCollect.js toArray() Method

Collect.js toArray() Method

The toArray() method is used to convert the given collection into a normal array. An array containing the values will be returned if the collection is an object.

Syntax:

collect(array).toArray()

Parameters: The collect() method takes one argument that is converted into the collection and the toArray() method is applied to it.

Return Value: This method returns an array of the given collection.

Below example illustrate the toArray() method in collect.js:

Example 1:

Javascript




const collect = require('collect.js'); 
  
let arr = ['kripa', 'biltu', 'chinu', 1, 2, 3]; 
    
const collection = collect(arr); 
    
const newObject = collection.toArray(); 
  
console.log(newObject);


Output:

["kripa", "biltu", "chinu", 1, 2, 3]

Example 2:

Javascript




const collect = require('collect.js');
  
let obj =
{
    name: 'TATA',
    companies: [
        'tcs', 'tata motors',
        'tata tea', 'tata salt', 'sonata'
    ],
};
  
const collection = collect(obj);
  
const newObject = collection.toArray();
  
console.log(newObject);


Output:

[ 'TATA', [ 'tcs', 'tata motors', 'tata tea', 'tata salt', 'sonata' ] ]
RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7012 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS