Google News
logo
Collect.js Interview Questions
contains() : The contains() method determines whether the collection contains a given item :
const collection = collect({
  name: 'Free Time Learn',
  number: 12,
});

collection.contains('name');
// true

collection.contains('age');
// false

collection.contains('Free Time Learn');
// true
You may also work with arrays
const collection = collect([1, 2, 3]);

collection.contains(3);
// true
You may also pass a key / value pair to the contains method, which will determine if the given pair exists in the collection:
const collection = collect({
  name: 'Free Time Learn',
  number: 12,
});

collection.contains('name', 'Steve Jobs');
// false
Finally, you may also pass a callback to the contains method to perform your own truth test:
const collection = collect([1, 2, 3, 4, 5]);

collection.contains((value, key) => value > 5);

// false
containsOneItem() : The containsOneItem() method returns true if the collection contains exactly one item; otherwise, false is returned.
collect([1]).containsOneItem();
// true

collect({ firstname: 'Luis' }).containsOneItem();
// true

collect('value').containsOneItem();
// true

collect([1, 2, 3]).containsOneItem();
//  false

collect({ firstname: 'Luis', lastname: 'Díaz' }).containsOneItem();
// false

collect().containsOneItem();
// false

collect([]).containsOneItem();
// false

collect({}).containsOneItem();
// false
count() : The count() method returns the total number of items in the collection.
const collection = collect([1, 2, 3, 4]);

collection.count();

// 4
countBy() : The countBy() method counts the occurences of values in the collection. By default, the method counts the occurrences of every element:
const collection = collect([1, 2, 2, 2, 3]);

const counted = collection.countBy();

counted.all();

// {
//   1: 1,
//   2: 3,
//   3: 1,
// }
However, you pass a callback to the countBy() method to count all items by a custom value:
const collection = collect([
  'freetimelearn@gmail.com',
  'info@freetimelearning.com',
  'vvramanareddy9963@gmail.com',
]);

const counted = collection.countBy(email => email.split('@')[1]);

counted.all();

// {
//   'gmail.com': 2,
//   'freetimelearning.com': 1,
// }
crossJoin() : The crossJoin() method cross joins the collection with the given array or collection, returning all possible permutations:
const collection = collect([1, 2]);

const joined = collection.crossJoin(['a', 'b']);

joined.all();

// [
//   [1, 'a'],
//   [1, 'b'],
//   [2, 'a'],
//   [2, 'b'],
// ]
dd() : The dd() method will console.log the collection and exit the current process:
const collection = collect([1, 2, 3]).dd();

// Collection { items: [ 1, 2, 3 ] }
// (Exits node.js process)
 
diff() : The diff() method compares the collection against another collection or a plain array based on its values. This method will return the values in the original collection that are not present in the given collection:
const collection = collect([1, 2, 3, 4, 5]);

const diff = collection.diff([1, 2, 3, 9]);

diff.all();

// [4, 5]
diffAssoc()The diffAssoc() method compares the collection against another collection or a plain object based on its keys and values. This method will return the key / value pairs in the original collection that are not present in the given collection:
const collection = collect({
  color: 'orange',
  type: 'fruit',
  remain: 6,
});

const diff = collection.diffAssoc({
  color: 'yellow',
  type: 'fruit',
  remain: 3,
  used: 6,
});

diff.all();

// { color: 'orange', remain: 6 };
 
diffKeys() : The diffKeys() method compares the collection against another collection or a plain object based on its keys. This method will return the key / value pairs in the original collection that are not present in the given collection:
const collection = collect({
  a: 'a',
  b: 'b',
  c: 'c',
  d: 'd',
});

const diff = collection.diffKeys({
  b: 'b',
  d: 'd',
});

diff.all();

// { a: 'a', c: 'c' }
doesntContain() : The doesntContain() method determines whether the collection does not contain a given item. You may pass a closure to the doesntContain method to determine if an element does not exist in the collection matching a given truth test:
const collection = collect([1, 2, 3, 4, 5]);

collection.doesntContain(value => value < 5);

// false
const collection = collect([1, 2, 3]);

collection.doesntContain(4);
// true
You may also use doesntContain on object based collections:
const collection = collect({
  name: 'Free Time Learn',
  number: 12,
});

collection.doesntContain('Free Time Learn');
// false

collection.doesntContain('Darwin Núñez');
// true
You may also pass a key / value pair to the contains method, which will determine if the given pair exists in the collection:
const collection = collect({
  name: 'Free Time Learn',
  number: 11,
});

collection.doesntContain('name', 'Darwin Núñez');
// true
dump() : The dump() method outputs the results at that moment and then continues processing:
collect([1, 2, 3, 4])
  .dump()
  .map(item => item * 2)
  .dump();

// Collection { items: [ 1, 2, 3, 4 ] }
// Collection { items: [ 2, 4, 6, 8 ] }
duplicates() : The duplicates() method retrieves and returns duplicate values from the collection:
const collection = collect(['a', 'b', 'a', 'c', 'b']);

const duplicates = collection.duplicates();

duplicates.all();

// { 2: 'a', 4: 'b' }
each() : The each() method iterates over the items in the collection and passes each item to a callback:
let sum = 0;

const collection = collect([1, 3, 3, 7]);

collection.each((item) => {
  sum += item;
});

// console.log(sum);
// 14
If you would like to stop iterating through the items, you may return false from your callback:
let sum = 0;

const collection = collect([1, 3, 3, 7]);

collection.each((item) => {
  sum += item;

  if (sum > 5) {
    return false;
  }
});

// console.log(sum);
// 7
eachSpread()The eachSpread() method iterates over the collection's items, passing each nested item value into the given callback:
const collection = collect([['John Doe', 35], ['Jane Doe', 33]]);

collection.eachSpread((name, age) => {
  //
});

You may stop iterating through the items by returning false from the callback:

collection.eachSpread((name, age) => false);
every() : The every() method may be used to verify that all elements of a collection pass a given truth test:
collect([1, 2, 3, 4]).every((value, key) => value > 2);

// false
except() : The except() method returns all items in the collection except for those with the specified keys:
const collection = collect({
  product_id: 1,
  price: 100,
  discount: false,
});

const filtered = collection.except(['price', 'discount']);

filtered.all();

// { product_id: 1 }
collect([1, 2, 3, 4])
  .except([2, 12])
  .all();

// [1, 3, 4]
For the inverse of except, see the only method.

Sources : Collect.js, more,.