Google News
logo
Collect.js Interview Questions
unique() : The unique() method returns all of the unique items in the collection:
const collection = collect([1, 1, 1, 2, 3, 3]);

const unique = collection.unique();

unique.all();

// [1, 2, 3]
When dealing with an array of objects, you may specify the key used to determine uniqueness:
const collection = collect([
  { name: 'iPhone 6', brand: 'Apple', type: 'phone' },
  { name: 'iPhone 5', brand: 'Apple', type: 'phone' },
  { name: 'Apple Watch', brand: 'Apple', type: 'watch' },
  { name: 'Galaxy S6', brand: 'Samsung', type: 'phone' },
  { name: 'Galaxy Gear', brand: 'Samsung', type: 'watch' },
]);

const unique = collection.unique('brand');

unique.all();

// [
//   { name: 'iPhone 6', brand: 'Apple', type: 'phone' },
//   { name: 'Galaxy S6', brand: 'Samsung', type: 'phone' },
// ]
You may also pass your own callback to determine item uniqueness :
const collection = collect([
  { name: 'iPhone 6', brand: 'Apple', type: 'phone' },
  { name: 'iPhone 5', brand: 'Apple', type: 'phone' },
  { name: 'Apple Watch', brand: 'Apple', type: 'watch' },
  { name: 'Galaxy S6', brand: 'Samsung', type: 'phone' },
  { name: 'Galaxy Gear', brand: 'Samsung', type: 'watch' },
]);

const unique = collection.unique(item => item.brand + item.type);

unique.all();

// [
//   { name: 'iPhone 6', brand: 'Apple', type: 'phone' },
//   { name: 'Apple Watch', brand: 'Apple', type: 'watch' },
//   { name: 'Galaxy S6', brand: 'Samsung', type: 'phone' },
//   { name: 'Galaxy Gear', brand: 'Samsung', type: 'watch' },
// ]
unless() : The unless() method will execute the given callback when the first argument given to the method evaluates to false:
const collection = collect([1, 2, 3]);

collection.unless(false, items => items.push(4));

collection.all();

// [1, 2, 3, 4]

unlessEmpty() : Alias for the whenNotEmpty() method
 
unlessNotEmpty() : Alias for the whenEmpty() method
when() : The when() method will execute the given callback when the first argument given to the method evaluates to true:
const collection = collect([1, 2, 3]);

collection.when(true, items => items.push(4));

collection.all();

// [1, 2, 3, 4]

whenEmpty() : The whenEmpty() method will execute the given callback when the collection is empty:
const collection = collect([]);

collection.whenEmpty(c => c.push('Mohamed Salah'));

collection.all();

// ['Mohamed Salah']
const collection = collect(['Darwin Núñez']);

collection.whenEmpty(
  c => c.push('Mohamed Salah'),
  c => c.push('Xherdan Shaqiri'),
);

collection.all();

// [
//   'Darwin Núñez',
//   'Xherdan Shaqiri',
// ];

whenNotEmpty()
The whenNotEmpty() method will execute the given callback when the collection is not empty:
const collection = collect(['Darwin Núñez']);

collection.whenNotEmpty(c => c.push('Mohamed Salah'));

collection.all();

// [
//   'Darwin Núñez',
//   'Mohamed Salah',
// ]
const collection = collect(['Darwin Núñez']);

collection.whenNotEmpty(
  c => c.push('Mohamed Salah'),
  c => c.push('Xherdan Shaqiri'),
);

collection.all();

// [
//   'Darwin Núñez',
//   'Mohamed Salah',
// ];
where() : The where() method filters the collection by a given key / value pair:
const collection = collect([
  { product: 'Desk', price: 200, discounted: true },
  { product: 'Chair', price: 100, discounted: true },
  { product: 'Bookcase', price: 150, discounted: true },
  { product: 'Door', price: 100 },
]);

const filtered = collection.where('price', 100);

filtered.all();

// [
//   { product: 'Chair', price: 100 },
//   { product: 'Door', price: 100 },
// ]

const discounted = collection.where('discounted');

discounted.all();

// [
//  { product: 'Desk', price: 200, discounted: true },
//  { product: 'Chair', price: 100, discounted: true },
//  { product: 'Bookcase', price: 150, discounted: true },
// ]

const notDiscounted = collection.where('discounted', false);

discounted.all();

// [
//  { product: 'Door', price: 100 },
// ]
When working with nested objects where() method allows dot notated keys. E.g. where('product.category', 'office-supplies') The where method also allows for custom comparisons:
 
Non-identity / strict inequality (!==) : 
const filtered = collection.where('price', '!==', 100);

filtered.all();

// [
//   { product: 'Desk', price: 200 },
//   { product: 'Bookcase', price: 150 },
// ]

Less than operator (<) : 
const filtered = collection.where('price', '<', 100);

filtered.all();

// []

Less than or equal operator (<=) : 
const filtered = collection.where('price', '<=', 100);

filtered.all();

// [
//   { product: 'Chair', price: 100 },
//   { product: 'Door', price: 100 },
// ]

Greater than operator (>) : 
const filtered = collection.where('price', '>', 100);

filtered.all();

// [
//   { product: 'Desk', price: 200} ,
//   { product: 'Bookcase', price: 150 },
// ]

Greater than or equal operator (>=) : 
const filtered = collection.where('price', '>=', 150);

filtered.all();

// [
//   { product: 'Desk', price: 200} ,
//   { product: 'Bookcase', price: 150 },
// ]

Sources : Collect.js, more,.