The Wayback Machine - https://web.archive.org/web/20240717142741/https://www.geeksforgeeks.org/collect-js/
Open In App

Collect.js

Last Updated : 16 Jun, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Collect.js is the javascript library for collecting data from tree-based structures. This library is used on JavaScript Array and Objects.

Collect.js Tutorial

Why Collect.js?

It is a fluent and convenient wrapper for working with arrays and objects. It provides us with different functions that help in working with data much easier. It also helps the programmers to write more concise code and easier to maintain the JavaScript code.

Installation: We can install it by using npm. To install it, first, make sure that you have installed Node.js and npm.

npm install collect-js

Using CDN Link: We can use CDN Link to run the code instead of installing it. Go to https://cdnjs.com/libraries/collect.js and add the following CDN link inside the head section.

<script src=”https://cdnjs.cloudflare.com/ajax/libs/collect.js/4.29.3/collect.min.js” integrity=”sha512eHrXl+YqedGIdIKP5YOueP0z7RfRgmo7R1d8pTvEOhikZIFpD54dXbwp9os9z4hVHVHZeRHw0qfF93lDsQDQA==” crossorigin=”anonymous” referrerpolicy=”no-referrer”></script>

Example: In this example, we will get the array or object represented by the collection using all() method.

JavaScript
const collect = require('collect.js');

let arr = [1, 2, 3]

// Convert array into collection
const collection = collect(arr);

// Returning the array
let newObject = collection.all();

console.log("Result : ", newObject);

Output:

Result : [1, 2, 3]

Learn More:


Similar Reads

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 met
1 min read
Collect.js replace() Method
The replace() method is used to replace the elements of the original collection that match the given string or numeric keys. If the key given in the object matches with a key in the collection, then its value is replaced, otherwise, if the key does not match, then it is added to the collection. Syntax: collect(array).replace(object) Parameters: The
1 min read
Collect.js reject() Method
The reject() method is used to filter the given collection of elements using the given callback function. If the callback function returns true, then the element is removed from the resulting collection, otherwise, it is not removed. Syntax: collect(array).reject(callback) Parameters: The collect() method takes one argument that is converted into t
1 min read
Collect.js | avg() Method
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 avg() method returns the average of all the items in a collection. Installation: Collect.js can be installed via NPM: npm install --save collect.js You
2 min read
Collect.js | combine() Method
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 combine() method combines two different arrays with keys-values. Installation: Collect.js can be installed via NPM: npm install --save collect.jsYou can
1 min read
Collect.js | crossJoin() Method
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 crossJoin() method cross joins the collection with an array or collection and return all possible permutations between them. Installation: Collect.js c
1 min read
Collect.js | collapse() Method
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: Collect.js can be installed via NPM: npm install --save collect.
2 min read
Collect.js | transform() Function
The transform() function iterates over a collection and callback each item in the collection, the items inside the collection are replaced by the new callback values. It is similar to the JavaScript map() function but the values got replaced in transform() function. In JavaScript, the array is first converted to a collection and then the function i
2 min read
Collect.js | all() Method
The all() method is used to return the underlying array or object represented by the collection. The JavaScript array is first transformed into a collection and then the function is applied to the collection. Syntax: collect(array).all() Parameters: The method does not accept any parameter. Return Value: Returns an array or object. Below examples i
1 min read
Collect.js | chunk() Function
The chunk() function breaks the collection to many small collections of the given size. In JavaScript, the array is first converted to a collection and then the function is applied to the collection. Syntax: data.chunk(x) Parameter: This function accepts a single parameter as mentioned above and described below. x : This parameter holds an integer
2 min read
three90RightbarBannerImg