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