Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Collect.js toArray() Method

  • Last Updated : 03 Dec, 2020

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.



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:


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


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

Example 2:


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();


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

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!