Collect.js

  • Last Updated : 30 Dec, 2021

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:

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

My Personal Notes arrow_drop_up


Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.