Google News
logo
Collect.js Interview Questions
Collect.js is the javascript library for collecting data from tree-based structures. This library is used on JavaScript Array and Objects. Collect.js framework that allows merchants to collect sensitive payment information from their customers without exposing their website to sensitive information.
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.
All comparisons in collect.js are done using strict equality. Using loose equality comparisons are generally frowned upon in JavaScript. Laravel only performs "loose" comparisons by default and offer several "strict" comparison methods. These methods have not been implemented in collect.js because all methods are strict by default.
Installation :

NPM
npm install collect.js --save
Yarn
yarn add collect.js
From CDN
* Add CDN link to your site with <script>

Using build / minified version
* Download collect.min.js
* Add to your site with <script>
The all method returns the underlying array or object represented by the collection :
collect([1, 2, 3]).all();

// [1, 2, 3]
collect({
  firstname: 'Darwin',
  lastname: 'Núñez',
}).all();

// {
//   firstname: 'Darwin',
//   lastname: 'Núñez',
// }
The average() method is used to return the average of all the items in a collection. This method is an alias of avg() method.
 
Syntax :
collect(array).average()
Parameters : The collect() method takes one argument that is converted into the collection and then average() function is applied on it, which can take element if you apply it on the collection of objects.

Example : 
const collect = require('collect.js');
  
let arr = [10, 20, 30];
  
let average = collect(arr).average();
  
console.log("Average of the given array: ", average);
Output:
Average of the given array:  20
The avg() method returns the average of all the items in a collection.
 
The collect() takes one argument that is converted into the collection and then avg() function is applied on it, which can take element if you apply it on the collection of objects.
 
avg() method example :
const collect = require('collect.js');
  
let arr = [
    {
      name: 'Ramana',
      score: 98,
    },
    {
      name: 'Venkat',
      score: 96,
    },
    {
        name: 'Suresh',
        score: 80
    },
  ];
  
// converting object to collection
const collection = collect(arr);
    
// finding the average of all the score
let averageScore = collection.avg('score');
    
console.log("Average score of students : ", averageScore);
Output :
Average score of students : 91.33333333333
The chunk() method breaks the collection into multiple, smaller collections of a given size :
Syntax : 
data.chunk(x)

Example : 

const collect = require('collect.js');
const collection = collect([1, 2, 3, 4, 5, 6, 7]);
const x = collection.chunk(5);
console.log(x.all());

Output : 

 [ Collection { items: [ 1, 2, 3, 4, 5 ] },
  Collection { items: [ 6, 7 ] } ]

 

collapse() : The collapse() method collapses a collection of arrays into a single, flat collection :
const collection = collect([[1], [{}, 5, {}], ['xoxo']]);

const collapsed = collection.collapse();

collapsed.all();

// [1, {}, 5, {}, 'xoxo']
const collection = collect([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);

const collapsed = collection.collapse();

collapsed.all();

// [1, 2, 3, 4, 5, 6, 7, 8, 9]
 
combine() : The combine() method combines the keys of the collection with the values of another array or collection :
const collection = collect(['name', 'number']);

const combine = collection.combine(['Free Time Learn', 12]);

combine.all();

// {
//   name: 'Free Time Learn',
//   number: 12
// }
concat() : The concat() method is used to merge two or more collections/arrays/objects :
 
You can also concat() an array of objects, or a multidimensional array
const collection = collect([1, 2, 3]);

let concatenated = collection.concat(['a', 'b', 'c']);

concatenated = concatenated.concat({
  name: 'Free Time Learning',
  number: 12,
});

concatenated.all();

// [1, 2, 3, 'a', 'b', 'c', 'Free Time Learn', 12]

Sources : Collect.js, more,.