Collect.js is a fluent and convenient wrapper for working with arrays and objects. The JavaScript array is first transformed into a collection and then the function is applied to the collection.
The
collapse() method convert collection of arrays into a single flat array.
Installation:
Syntax:
collect(array).collapse()
Parameters: The collect() takes one argument that is converted into the collection and then collapse() function is applied on it, which will convert into a single flat array.
Return Value: Returns an array.
Below examples illustrate the
collapse() method in JavaScript:
Example 1: Here collect = require('collect.js') is used to import the collect.js library into the file.
Output
👁 Image
Example 2: