Google News
logo
Collect.js Interview Questions
replace() : The replace() method behaves similarly to merge; however, in addition to overwriting matching items with string keys, the replace method will also overwrite items in the collection that have matching numeric keys:
const collection = collect({
  name: 'Bob',
});

const replaced = collection.replace({
  name: 'John',
  number: 45,
});

replaced.all();

// {
//   name: 'John',
//   number: 45,
// }
replaceRecursive() : This method works like replace, but it will recurse into arrays and apply the same replacement process to the inner values:
const collection = collect([
  'Matip',
  'van Dijk',
  [
    'Núñez',
    'Firmino',
    'Salah',
  ],
]);

const replaced = collection.replaceRecursive({
  0: 'Gomez',
  2: { 1: 'Origi' },
});

replaced.all();

// {
//   0: 'Gomez',
//   1: 'van Dijk',
//   2: { 0: 'Núñez', 1: 'Origi', 2: 'Salah' },
// }
search() : The search() method searches the collection for the given value and returns its key if found. If the item is not found, false is returned.
const collection = collect([2, 4, 6, 8]);

collection.search(4);

// 1
The search is done using a "loose" comparison, meaning a string with an integer value will be considered equal to an integer of the same value. To use strict comparison, pass true as the second argument to the method :
collection.search('4', true);

// false
Alternatively, you may pass in your own callback to search for the first item that passes your truth test:

collection.search((item, key) => item > 5);

// 2
shift() : The shift() method removes and returns the first item from the collection :
const collection = collect([1, 2, 3, 4, 5]);

collection.shift();

// 1

collection.all();

// [2, 3, 4, 5]
You may provide number of items to shift. This will return the shifted items in a new collection :
const collection = collect([1, 2, 3, 4, 5]);

collection.shift(2).all();

// [1, 2]

collection.all();

// => [3, 4, 5]
shuffle() : The shuffle() method randomly shuffles the items in the collection:
const collection = collect([1, 2, 3, 4, 5]);

const shuffled = collection.shuffle();

shuffled.all();

// [3, 5, 1, 2, 4] (generated randomly)

 

skip() : The skip() method returns a new collection, without the first given amount of items:
const collection = collect([1, 2, 3, 4, 5, 6]);

collection.skip(4).all();

// [5, 6]
const collection = collect({
  first: 'first',
  second: 'second',
  third: 'third',
  fourth: 'fourth',
  fifth: 'fifth',
});

collection.skip(4).all();

// { fifth: 'fifth' }
skipUntil() : The skipUntil() method skips items until the given callback returns true and then returns the remaining items in the collection:
const collection = collect([1, 2, 3, 4]);

const subset = collection.skipUntil(item => item >= 3);

subset.all();

// [3, 4]
You may also pass a simple value to the skipUntil method to skip all items until the given value is found:
const collection = collect([1, 2, 3, 4]);

const subset = collection.skipUntil(3);

subset.all();

// [3, 4]

skipWhile() : The skipWhile() method skips items while the given callback returns true and then returns the remaining items in the collection:
const collection = collect([1, 2, 3, 4]);

const subset = collection.skipWhile(item => item <= 3);

subset.all();

// [4]
If the callback never returns true, the skipWhile() method will return an empty collection.
 
You may also pass a simple value to the skipWhile:
const collection = collect([1, 1, 2, 2, 3, 3, 4, 4]);

const subset = collection.skipWhile(1);

subset.all();

// [2, 2, 3, 3, 4, 4]
sort() : The sort() method sorts the collection:
const collection = collect([5, 3, 1, 2, 4]);

const sorted = collection.sort();

sorted.all();

// [1, 2, 3, 4, 5]
If your sorting needs are more advanced, you may pass a callback to sort with your own algorithm.
const collection = collect([5, 3, 1, 2, 4]);

const sorted = collection.sort((a, b) => b - a);

sorted.all();

// [5, 4, 3, 2, 1]
If you need to sort a collection of nested arrays or objects, see the sortBy and sortByDesc methods.
 

sortBy() : The sortBy() method sorts the collection by the given key. The sorted collection keeps the original array keys, so in this example we'll use the values method to reset the keys to consecutively numbered indexes:
const collection = collect([
  { name: 'Desk', price: 200 },
  { name: 'Chair', price: 100 },
  { name: 'Bookcase', price: 150 },
]);

const sorted = collection.sortBy('price');

sorted.all();

// [
//   { name: 'Chair', price: 100 },
//   { name: 'Bookcase', price: 150 },
//   { name: 'Desk', price: 200 },
// ]
You can use dot notation to sort by nested values
const collection = collect([
  {
    name: 'Desk',
    price: 200,
    manufacturer: {
      name: 'IKEA',
    },
  },
  {
    name: 'Chair',
    price: 100,
    manufacturer: {
      name: 'Herman Miller',
    },
  },
  {
    name: 'Bookcase',
    price: 150,
    manufacturer: {
      name: 'IKEA',
    },
  },
]);

const sorted = collection.sortBy('manufacturer.name');

sorted.all();

// [
//   {
//     name: 'Chair',
//     price: 100,
//     manufacturer: {
//       name: 'Herman Miller',
//     },
//   },
//   {
//     name: 'Desk',
//     price: 200,
//     manufacturer: {
//       name: 'IKEA',
//     },
//   },
//   {
//     name: 'Bookcase',
//     price: 150,
//     manufacturer: {
//       name: 'IKEA',
//     },
//   },
// ]
 
You can also pass your own callback to determine how to sort the collection values:
const collection = collect([
  { name: 'Desk', colors: ['Black', 'Mahogany'] },
  { name: 'Chair', colors: ['Black'] },
  { name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] },
]);

const sorted = collection.sortBy((product, key) => product.colors.length);

sorted.all();

// [
//   { name: 'Chair', colors: ['Black'] },
//   { name: 'Desk', colors: ['Black', 'Mahogany'] },
//   { name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] },
// ]

sortByDesc()  : This method has the same signature as the sortBy method, but will sort the collection in the opposite order.
 
sortDesc()  : This method will sort the collection in the opposite order as the sort method.
const collection = collect([1, 3, 5, 2, 4]);

const sorted = collection.sortDesc();

sorted.all();

// [5, 4, 3, 2, 1]
Unlike sort, you may not pass a callback to sortDesc. If you wish to use a callback, you should use sort and invert your comparison.
 

sortKeys()
The sortKeys() method sorts the collection by the keys of the underlying associative array:
const collection = collect({
  id: 10,
  first: 'Darwin',
  last: 'Núñez',
});

const sorted = collection.sortKeys();

sorted.all();

// {
//   first: 'Darwin',
//   id: 10,
//   last: 'Núñez',
// }

sortKeysDesc()
This method has the same signature as the sortKeys method, but will sort the collection in the opposite order.
sum()The sum() method returns the sum of all items in the collection :
collect([1, 2, 3]).sum();

// 6
If the collection contains nested arrays or objects, you should pass a key to use for determining which values to sum :
const collection = collect([
  { name: 'My story', pages: 176 },
  { name: 'Fantastic Beasts and Where to Find Them', pages: 1096 },
]);

collection.sum('pages');

// 1272
In addition, you may pass your own callback to determine which values of the collection to sum :
const collection = collect([
  { name: 'Desk', colors: ['Black', 'Mahogany'] },
  { name: 'Chair', colors: ['Black'] },
  { name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] },
]);

const total = collection.sum(product => product.colors.length);

// 6
take() : The take() method returns a new collection with the specified number of items: You may also pass a negative integer to take the specified amount of items from the end of the collection:
const collection = collect([0, 1, 2, 3, 4, 5]);

const chunk = collection.take(3);

chunk.all();

// [0, 1, 2]
 
takeUntil() : The takeUntil() method returns items in the collection until the given callback returns true:
const collection = collect([1, 2, 3, 4]);

const subset = collection.takeUntil(item => item >= 3);

subset.all();

// [1, 2]
If the given value is not found or the callback never returns true, the takeUntil method will return all items in the collection.
 
You may also pass a simple value to the takeUntil() method to get the items until the given value is found:
const collection = collect([1, 2, 3, 4]);

const subset = collection.takeUntil(3);

subset.all();

// [1, 2]
takeWhile() : The takeWhile() method returns items in the collection until the given callback returns false:
const collection = collect([1, 2, 3, 4]);

const subset = collection.takeWhile(item => item < 3);

subset.all();

// [1, 2]
If the callback never returns false, the takeWhile method will return all items in the collection.
times() : The times() method creates a new collection by invoking the callback a given amount of times:
const collection = collect().times(10, number => number * 9);

collection.all();

// [9, 18, 27, 36, 45, 54, 63, 72, 81, 90]
toArray() : The toArray() method converts the collection into a plain array. If the collection is an object, an array containing the values will be returned.
const collection = collect([1, 2, 3, 'b', 'c']);

collection.toArray();

// [1, 2, 3, 'b', 'c']
const collection = collect({
  name: 'Elon Musk',
  companies: ['Tesla', 'Space X', 'SolarCity'],
});

collection.toArray();

// ['Elon Musk', ['Tesla', 'Space X', 'SolarCity']]​
toJson() : The toJson() method converts the collection into JSON string:
const collection = collect({
  id: 384,
  name: 'Rayquaza',
  gender: 'NA',
});

const json = collection.toJson();

// {"id": 384, "name": "Rayquaza", "gender": "NA"}

Sources : Collect.js, more,.