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. 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. npm install collect.js --save
yarn add collect.js
<script>
collect.min.js
<script>
collect([1, 2, 3]).all();
// [1, 2, 3]
collect({
firstname: 'Darwin',
lastname: 'Núñez',
}).all();
// {
// firstname: 'Darwin',
// lastname: 'Núñez',
// }
average()
method is used to return the average of all the items in a collection. This method is an alias of avg()
method.collect(array).average()
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.const collect = require('collect.js');
let arr = [10, 20, 30];
let average = collect(arr).average();
console.log("Average of the given array: ", average);
Average of the given array: 20
avg()
method returns the average of all the items in a collection.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);
Average score of students : 91.33333333333
chunk() method
breaks the collection into multiple, smaller collections of a given size :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 :concat()
an array of objects, or a multidimensional arrayconst 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,.