Giter Site home page Giter Site logo

ecrmnn / collect.js Goto Github PK

View Code? Open in Web Editor NEW
6.5K 77.0 319.0 1.22 MB

💎  Convenient and dependency free wrapper for working with arrays and objects

Home Page: https://collect.js.org

License: MIT License

JavaScript 100.00%
collection laravel nested-objects nested-arrays laravel-collections

collect.js's Introduction

collect.js

Convenient and dependency free wrapper for working with arrays and objects

Build Status npm version npm downloads npm license PRs Welcome dependencies eslint cdnjs version

Installation

NPM

npm install collect.js --save

Yarn

yarn add collect.js

From CDN

  1. Visit https://cdnjs.com/libraries/collect.js
  2. Add CDN link to your site with <script>

Using build / minified version

  1. Download collect.min.js
  2. Add to your site with <script>

Tip

Using Laravel as your backend? Collect.js offers an (almost) identical api to Laravel Collections. See differences.

API

All available methods

Strictness and comparisons

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.

Methods that have not been implemented:
  • containsStrict use contains()
  • duplicatesStrict use duplicates()
  • uniqueStrict use unique()
  • whereStrict use where()
  • whereInStrict use whereIn()
  • whereNotInStrict use whereNotIn()

all()

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',
// }

average()

Alias for the avg() method

avg()

The avg method returns the average of all items in the collection:

collect([1, 3, 3, 7]).avg();

// 3.5

If the collection contains nested arrays or objects, you should pass a key to use for determining which values to calculate the average:

const collection = collect([
  {
    name: 'My story',
    pages: 176,
  },
  {
    name: 'Fantastic Beasts and Where to Find Them',
    pages: 1096,
  },
]);

collection.avg('pages');

// 636

You may also define a callback function

const collection = collect([
  {
    name: 'My story',
    pages: 176,
  },
  {
    name: 'Fantastic Beasts and Where to Find Them',
    pages: 1096,
  },
]);

collection.avg(book => book.pages);

// 636

chunk()

The chunk method breaks the collection into multiple, smaller collections of a given size:

const collection = collect([1, 2, 3, 4, 5, 6, 7]);

const chunks = collection.chunk(4);

chunks.all();

// [[1, 2, 3, 4], [5, 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(['Mohamed Salah', 11]);

combine.all();

// {
//   name: 'Mohamed Salah',
//   number: 11
// }

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: 'Mohamed Salah',
  number: 11,
});

concatenated.all();

// [1, 2, 3, 'a', 'b', 'c', 'Mohamed Salah', 11]

contains()

The contains method determines whether the collection contains a given item:

const collection = collect({
  name: 'Mohamed Salah',
  number: 11,
});

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

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

collection.contains('Mohamed Salah');
// 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: 'Mohamed Salah',
  number: 11,
});

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([
  '[email protected]',
  '[email protected]',
  '[email protected]',
]);

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

counted.all();

// {
//   'gmail.com': 2,
//   'yahoo.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' }

diffUsing()

The diffUsing method compares the collection against another collection or a plain array based on its values using a callback. This method will return the values in the original collection that are not present in the given collection:

const collection = collect([
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 30 },
  { name: 'Charlie', age: 35 },
]);

const users = [
  { name: 'Bob', age: 30 },
  { name: 'Charlie', age: 35 },
  { name: 'David', age: 40 },
];

const diff = collection.diffUsing(users, (a, b) => a.age - b.age);

diff.all();

// [{ name: 'Alice', age: 25 }]

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: 'Mohamed Salah',
  number: 11,
});

collection.doesntContain('Mohamed Salah');
// 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: 'Mohamed Salah',
  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.

filter()

The filter method filters the collection using the given callback, keeping only those items that pass a given truth test:

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

const filtered = collection.filter((value, key) => value > 2);

filtered.all();

// [3, 4]

If no callback is supplied, all entries of the collection that are equivalent to false will be removed:

const collection = collect([
  0,
  1,
  2,
  null,
  3,
  4,
  undefined,
  5,
  6,
  7,
  [],
  8,
  9,
  {},
  10,
]);

const filtered = collection.filter();

filtered.all();

// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

For the inverse of filter, see the reject method.

first()

The first method returns the first element in the collection that passes a given truth test:

collect([1, 2, 3, 4]).first(item => item > 1);

// 2

You may also call the first method with no arguments to get the first element in the collection. If the collection is empty, null is returned:

collect([1, 2, 3, 4]).first();

// 1

firstOrFail()

The firstOrFail method returns the first element in the collection, or throws an error if there are no elements:

collect([1, 2, 3, 4]).firstOrFail(item => item > 1);

// 2
collect([1, 2, 3, 4]).firstOrFail(item => item > 4);

// Error ('Item not found.') is thrown.

You may also call the firstOrFail method with no arguments to get the first element in the collection. If the collection is empty, an error is thrown:

collect([1, 2, 3, 4]).firstOrFail();

// 1
collect().firstOrFail();

// Error ('Item not found.') is thrown.

Like the where method, you may also pass an attribute, operator, and value:

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 },
]);

collection.firstOrFail('product', '=', 'Desk');

// { product: 'Desk', price: 200, discounted: true }

firstWhere()

The firstWhere method returns the first element in the collection with the given key / value pair:

const collection = collect([
  { name: 'Regena', age: 12 },
  { name: 'Linda', age: 14 },
  { name: 'Diego', age: 23 },
  { name: 'Linda', age: 84 },
]);

collection.firstWhere('name', 'Linda');

// { name: 'Linda', age: 14 }

flatMap()

The flatMap method iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items. Then, the array is flattened by a level:

const collection = collect([
  {
    name: 'Darwin NĂșñez',
    number: 27,
  },
  {
    name: 'Mohamed Salah',
    number: 11,
  },
]);

const flatMapped = collection.flatMap(value => value.name.toUpperCase());

flatMapped.all();

// ['DARWIN NÚÑEZ', 'MOHAMED SALAH']

flatten()

The flatten method flattens a multi-dimensional collection into a single dimension:

const collection = collect({
  club: 'Liverpool',
  players: ['Salah', 'Firmino', 'NĂșñez'],
});

const flattened = collection.flatten();

flattened.all();

// ['Liverpool', 'Salah', 'Firmino', 'NĂșñez'];

You may optionally pass the function a "depth" argument:

const collection = collect({
  Apple: [
    {
      name: 'iPhone 6S',
      brand: 'Apple',
    },
  ],
  Samsung: [
    {
      name: 'Galaxy S7',
      brand: 'Samsung',
    },
  ],
});

const flattened = collection.flatten(1);

flattened.all();

// [
//   { name: 'iPhone 6S', brand: 'Apple' },
//   { name: 'Galaxy S7', brand: 'Samsung' },
// ]

In this example, calling flatten without providing the depth would have also flattened the nested arrays, resulting in ['iPhone 6S', 'Apple', 'Galaxy S7', 'Samsung']. Providing a depth allows you to restrict the levels of nested arrays that will be flattened.

flip()

The flip method swaps the collection's keys with their corresponding values:

const collection = collect({
  name: 'Darwin NĂșñez',
  number: 27,
});

const flipped = collection.flip();

flipped.all();

// {
//   'Darwin NĂșñez': 'name',
//   '27': 'number',
// }

forPage()

The forPage method returns a new collection containing the items that would be present on a given page number. The method accepts the page number as its first argument and the number of items to show per page as its second argument:

const collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]);

const forPage = collection.forPage(2, 3);

forPage.all();

// [4, 5, 6]

forget()

The forget method removes an item from the collection by its key:

const collection = collect({
  name: 'Darwin NĂșñez',
  number: 27,
});

collection.forget('number');

collection.all();

// {
//   name: 'Darwin NĂșñez',
// }

Unlike most other collection methods, forget does not return a new modified collection; it modifies the collection it is called on.

get()

The get method returns the item at a given key or index. If the key or index does not exist, null is returned:

const collection = collect({
  firstname: 'Mohamed',
  lastname: 'Salah',
});

collection.get('lastname');

// Salah

collection.get('middlename');

// null
const collection = collect(['a', 'b', 'c']);

collection.get(1);

// b

You may optionally pass a default value as the second argument:

const collection = collect({
  firstname: 'Mohamed',
  lastname: 'Salah',
});

collection.get('middlename', 'default-value');
// default-value

You may even pass a callback as the default value. The result of the callback will be returned if the specified key does not exist:

const collection = collect({
  firstname: 'Mohamed',
  lastname: 'Salah',
});

collection.get('middlename', () => 'default-value');

// default-value

groupBy()

The groupBy method groups the collection's items into multiple collections by a given key:

If you want to group the collection by keys as a plain object, see mapToGroups

const collection = collect([
  {
    product: 'Chair',
    manufacturer: 'IKEA',
  },
  {
    product: 'Desk',
    manufacturer: 'IKEA',
  },
  {
    product: 'Chair',
    manufacturer: 'Herman Miller',
  },
]);

const grouped = collection.groupBy('manufacturer');

grouped.all();

// {
//   IKEA: Collection {
//     items: [
//        {
//          id: 100,
//          product: 'Chair',
//          manufacturer: 'IKEA',
//          price: '1490 NOK',
//        },
//        {
//          id: 150,
//          product: 'Desk',
//          manufacturer: 'IKEA',
//          price: '900 NOK',
//        },
//      ],
//   },
//   'Herman Miller': Collection {
//     items: [
//       {
//         id: 200,
//         product: 'Chair',
//         manufacturer: 'Herman Miller',
//         price: '9990 NOK',
//       },
//     ],
//   },
// }

In addition to passing a string key, you may also pass a callback. The callback should return the value you wish to key the group by:

const collection = collect([
  {
    product: 'Chair',
    manufacturer: 'IKEA',
  },
  {
    product: 'Desk',
    manufacturer: 'IKEA',
  },
  {
    product: 'Chair',
    manufacturer: 'Herman Miller',
  },
]);

const grouped = collection.groupBy((item, key) => item.manufacturer.substring(0, 3));

grouped.all();

// {
//   IKE: Collection {
//     items: [
//       {
//         id: 100,
//         product: 'Chair',
//         manufacturer: 'IKEA',
//         price: '1490 NOK',
//       },
//       {
//         id: 150,
//         product: 'Desk',
//         manufacturer: 'IKEA',
//         price: '900 NOK',
//       },
//     ],
//   },
//   Her: Collection {
//     items: [
//       {
//         id: 200,
//         product: 'Chair',
//         manufacturer: 'Herman Miller',
//         price: '9990 NOK',
//       },
//     ],
//   },
// }

has()

The has method determines if one or more keys exists in the collection:

const collection = collect({
  animal: 'unicorn',
  ability: 'magical',
});

collection.has('ability');

// true

collection.has(['animal', 'ability']);

// true

collection.has(['animal', 'ability', 'name']);

// false

implode()

The implode method joins the items in a collection. Its arguments depend on the type of items in the collection. If the collection contains arrays or objects, you should pass the key of the attributes you wish to join, and the "glue" string you wish to place between the values:

const collection = collect([
  {
    product: 'Chair',
    manufacturer: 'IKEA',
  },
  {
    product: 'Desk',
    manufacturer: 'IKEA',
  },
  {
    product: 'Chair',
    manufacturer: 'Herman Miller',
  },
]);

collection.implode('product', ',');

// Chair, Desk, Chair

If the collection contains simple strings or numeric values, simply pass the "glue" as the only argument to the method:

collect([1, 2, 3, 4, 5]).implode('-');

// 1-2-3-4-5

intersect()

The intersect method removes any values from the original collection that are not present in the given array or collection. The resulting collection will preserve the original collection's keys:

const collection = collect([1, 2, 3, 4, 5]);

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

intersect.all();

// [1, 2, 3]

intersectByKeys()

The intersectByKeys method removes any keys from the original collection that are not present in the given array or collection:

const collection = collect({
  serial: 'UX301',
  type: 'screen',
  year: 2009,
});

const intersect = collection.intersectByKeys({
  reference: 'UX404',
  type: 'tab',
  year: 2011,
});

intersect.all();

// { type: 'screen', year: 2009 }
const firstCollection = collect([1, 2, 3, 4, 5]);
const secondCollection = collect([1, 2, 3, 9]);

intersect = firstCollection.intersect(secondCollection);

intersect.all();

// [1, 2, 3]

isEmpty()

The isEmpty method returns true if the collection is empty; otherwise, false is returned:

collect().isEmpty();
// true

collect([]).isEmpty();
// true

collect({}).isEmpty();
// true

isNotEmpty()

The isNotEmpty method returns true if the collection is not empty; otherwise, false is returned:

collect([1, 2, 3]).isNotEmpty();
//  true

collect().isNotEmpty();
// false

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

collect({}).isNotEmpty();
// false

join()

The join method joins the collection's values with a string:

collect(['a', 'b', 'c']).join(', ');
// 'a, b, c'

collect(['a', 'b', 'c']).join(', ', ', and ');
// 'a, b, and c'

collect(['a', 'b']).join(', ', ' and ');
// 'a and b'

collect(['a']).join(', ', ' and ');
// 'a'

collect([]).join(', ', ' and ');
// ''

keyBy()

The keyBy method keys the collection by the given key. If multiple items have the same key, only the last one will appear in the new collection:

const collection = collect([
  {
    product: 'Chair',
    manufacturer: 'IKEA',
  },
  {
    product: 'Desk',
    manufacturer: 'IKEA',
  },
  {
    product: 'Chair',
    manufacturer: 'Herman Miller',
  },
]);

const keyed = collection.keyBy('manufacturer');

keyed.all();

// {
//   IKEA: {
//     product: 'Desk',
//     manufacturer: 'IKEA',
//   },
//   'Herman Miller': {
//     product: 'Chair',
//     manufacturer: 'Herman Miller',
//   },
// }

You may also pass a callback to the method. The callback should return the value to key the collection by:

const upperCased = collection.keyBy(item => item.manufacturer.toUpperCase());

upperCased.all();

// {
//   IKEA: {
//     product: 'Desk',
//     manufacturer: 'IKEA',
//   },
//   'HERMAN MILLER': {
//     product: 'Chair',
//     manufacturer: 'Herman Miller',
//   },
// }

keys()

The keys method returns all of the collection's keys:

const collection = collect([
  {
    club: 'Liverpool',
    nickname: 'The Reds',
  },
]);

keys = collection.keys();

// ['club', 'nickname']

last()

The last method returns the last element in the collection that passes a given truth test:

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

const last = collection.last(item => item > 1);

// 3

You may also call the last method with no arguments to get the last element in the collection. If the collection is empty, null is returned:

collect([1, 2, 3, 4]).last();

// 4

macro()

The macro method lets you register custom methods

collect().macro('uppercase', function () {
  return this.map(item => item.toUpperCase());
});

const collection = collect(['a', 'b', 'c']);

collection.uppercase();

collection.all();

// ['A', 'B', 'C']

Note that the macro method returns undefined, and therefore it is not possible to use it within a chain of methods.

make()

The make method creates a new collection instance.

This is only added to adhere to the Laravel collection API, when using Collect.js it's recommended to use collect() directly when creating a new collection.

map()

The map method iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items:

const collection = collect([1, 2, 3, 4, 5]);

const multiplied = collection.map(item => item * 2);

multiplied.all();

// [2, 4, 6, 8, 10]

Like most other collection methods, map returns a new collection instance; it does not modify the collection it is called on. If you want to transform the original collection, use the transform method.

mapInto()

The mapInto method iterates through the collection and instantiates the given class with each element as a constructor:

const Player = function (name) {
  this.name = name;
};

const collection = collect([
  'Roberto Firmino',
  'Darwin NĂșñez',
]);

const players = collection.mapInto(Player);

players.all();

// [
//   Player { name: 'Roberto Firmino' },
//   Player { name: 'Darwin NĂșñez' },
// ]

mapSpread()

The mapSpread method iterates over the collection's items, passing each nested item value into the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items:

const collection = collect([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

const chunks = collection.chunk(2);

const sequence = chunks.mapSpread((even, odd) => even + odd);

sequence.all();

// [1, 5, 9, 13, 17]

mapToDictionary()

Run a dictionary map over the items. The callback should return an associative array with a single key/value pair.

const collection = collect([
  { id: 1, name: 'a' },
  { id: 2, name: 'b' },
  { id: 3, name: 'c' },
  { id: 4, name: 'b' },
]);

const groups = collection.mapToDictionary(item => [item.name, item.id]);

groups.all();

// {
//   a: [1],
//   b: [2, 4],
//   c: [3],
// }

mapToGroups()

The mapToGroups method iterates through the collection and passes each value to the given callback:

const collection = collect([
  { id: 1, name: 'A' },
  { id: 2, name: 'B' },
  { id: 3, name: 'C' },
  { id: 4, name: 'B' },
]);

const groups = collection.mapToGroups((item, key) => [item.name, item.id]);

// {
//   A: [1],
//   B: [2, 4],
//   C: [3],
// }

mapWithKeys()

The mapWithKeys method iterates through the collection and passes each value to the given callback. The callback should return an array where the first element represents the key and the second element represents the value pair:

const collection = collect([
  {
    name: 'John',
    department: 'Sales',
    email: '[email protected]',
  },
  {
    name: 'Jane',
    department: 'Marketing',
    email: '[email protected]',
  },
]);

const keyed = collection.mapWithKeys(item => [item.email, item.name]);

keyed.all();

// {
//   '[email protected]': 'John',
//   '[email protected]': 'Jane',
// }

max()

The max method returns the maximum value of a given key:

const collection = collect([
  {
    value: 10,
  },
  {
    value: -13,
  },
  {
    value: 12,
  },
  {
    unicorn: false,
  },
]);

const max = collection.max('value');

// 12
collect([-1, -2345, 12, 11, 3]).max();

// 12

median()

The median method returns the median value of a given key:

collect([1, 3, 3, 6, 7, 8, 9]).median();

// 6
collect([
  {
    foo: 1,
  },
  {
    foo: 1,
  },
  {
    foo: 2,
  },
  {
    foo: 4,
  },
]).median('foo');

// 1.5

merge()

The merge method merges the given object into the original collection. If a key in the given object matches a key in the original collection, the given objects value will overwrite the value in the original collection:

const collection = collect({
  id: 1,
  price: 29,
});

const merged = collection.merge({
  price: 400,
  discount: false,
});

merged.all();

// { id: 1, price: 400, discount: false }

If our collection is an array, the values will be appended to the end of the collection:

const collection = collect(['Unicorn', 'Rainbow']);

const merged = collection.merge(['Sunshine', 'Rainbow']);

merged.all();

// ['Unicorn', 'Rainbow', 'Sunshine', 'Rainbow']

mergeRecursive()

The mergeRecursive method merges the given array or collection recursively with the original collection. If a string key in the given items matches a string key in the original collection, then the values for these keys are merged together into an array, and this is done recursively:

const collection = collect({
  product_id: 1,
  price: 100,
});

const merged = collection.mergeRecursive({
  product_id: 2,
  price: 200,
  discount: false,
});

merged.all();

// {
//   product_id: [1, 2],
//   price: [100, 200],
//   discount: false,
// }

min()

The min method returns the minimum value of a given key:

const collection = collect([
  {
    worth: 100,
  },
  {
    worth: 900,
  },
  {
    worth: 79,
  },
]);

collection.min('worth');

// 79
collect([1, 2, 3, 4, 5]).min();

// 1

mode()

The mode method returns the mode value of a given key:

collect([1, 3, 3, 6, 7, 8, 9]).mode();

// [3]
collect([
  {
    foo: 1,
  },
  {
    foo: 1,
  },
  {
    foo: 2,
  },
  {
    foo: 4,
  },
]).mode('foo');

// [1]

nth()

The nth method creates a new collection consisting of every n-th element:

const collection = collect(['a', 'b', 'c', 'd', 'e', 'f']);

const nth = collection.nth(4);

nth.all();

// ['a', 'e']

only()

The only method returns the items in the collection with the specified keys:

const collection = collect({
  id: 12,
  name: 'John Doe',
  email: '[email protected]',
  active: true,
});

const filtered = collection.only(['name', 'email']);

filtered.all();

// { name: 'John Doe', email: '[email protected]' }
collect([1, 2, 3, 4])
  .only([2, 12])
  .all();

// [2]

For the inverse of only, see the except method.

pad()

The pad method will fill the array with the given value until the array reaches the specified size. This method behaves like the array_pad PHP function.

To pad to the left, you should specify a negative size. No padding will take place if the absolute value of the given size is less than or equal to the length of the array:

const collection = collect(['A', 'B', 'C']);

let filtered = collection.pad(5, 0);

filtered.all();

// ['A', 'B', 'C', 0, 0]

filtered = collection.pad(-5, 0);

filtered.all();

// [0, 0, 'A', 'B', 'C']

partition()

The partition method may be combined with destructuring to separate elements that pass a given truth test from those that do not:

const collection = collect([1, 2, 3, 4, 5, 6]);

const [underThree, overThree] = collection.partition(i => i < 3);

pipe()

The pipe method passes the collection to the given callback and returns the result:

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

const piped = collection.pipe(items => items.sum());

// 6

pluck()

The pluck method retrieves all of the values for a given key:

const collection = collect([
  {
    id: 78,
    name: 'Aeron',
  },
  {
    id: 79,
    name: 'Embody',
  },
]);

const plucked = collection.pluck('name');

plucked.all();

// ['Aeron', 'Embody']

You may also specify how you wish the resulting collection to be keyed:

const collection = collect([
  {
    id: 78,
    name: 'Aeron',
  },
  {
    id: 79,
    name: 'Embody',
  },
]);

const plucked = collection.pluck('name', 'id');

plucked.all();

// {
//   78: 'Aeron',
//   79: 'Embody',
// }

You can use "dot notation" to access nested values

const collection = collect([
  {
    name: 'John',
    roles: [
      {
        name: 'Editor',
      },
      {
        name: 'Admin',
      },
    ],
  },
]);

const plucked = collection.pluck('roles.0.name');

plucked.all();

// ['Editor']

"Dot notation" supports "wildcard"

const collection = collect([
  {
    name: 'John',
    roles: [
      {
        name: 'Editor',
      },
      {
        name: 'Admin',
      },
    ],
  },
]);

const plucked = collection.pluck('roles.*.name');

plucked.all();

// [
//   [
//     'Editor',
//     'Admin',
//   ],
// ]

pop()

The pop method removes and returns the last item from the collection:

const collection = collect([1, 2, 3, 4, 5]);

collection.pop();

// 5

collection.all();

// => [1, 2, 3, 4]

You may provide number of items to pop. This will return the popped items in a new collection:

const collection = collect([1, 2, 3, 4, 5]);

collection.pop(2).all();

// [4, 5]

collection.all();

// => [1, 2, 3]

prepend()

The prepend method adds an item to the beginning of the collection:

const collection = collect([1, 2, 3, 4, 5]);

collection.prepend(0);

collection.all();

// [0, 1, 2, 3, 4, 5]

You may also pass a second argument to set the key of the prepended item:

Pro tip: Order of properties in objects is not guaranteed in JavaScript; When calling prepend with a key, the Collection uses the underlying put method behind the scenes. This is only supported so that collect.js have the same api as Laravel Collections.

const collection = collect({
  product: 'iPhone 6s',
});

collection.prepend('Apple', 'brand');

collection.all();

// {
//   brand: 'Apple',
//   product: 'iPhone 6s',
// }

pull()

The pull method removes and returns an item from the collection by its key:

const collection = collect({
  firstname: 'Michael',
  lastname: 'Cera',
});

collection.pull('lastname');

// Cera

collection.all();

// { firstname: 'Michael' }

push()

The push method appends an item to the end of the collection:

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

collection.push(5);

collection.all();

// [1, 2, 3, 4, 5]

put()

The put method sets the given key and value in the collection:

const collection = collect(['JavaScript', 'Python']);

collection.put('Ruby');

collection.all();

// ['JavaScript', 'Python', 'Ruby']

random()

The random method returns a random item from the collection:

const collection = collect([1, 2, 3, 4, 5]);

collection.random();

// 4 (retrieved randomly)

You may optionally pass an integer to random to specify how many items you would like to randomly retrieve. A collection of items is always returned when explicitly passing the number of items you wish to receive:

const threeRandom = collection.random(3);

// Collection { items: [ 5, 3, 4 ] } (retrieved randomly)

const oneRandom = collection.random(1);

// Collection { items: [ 3 ] } (retrieved randomly)

oneRandom.all();

// [3]

reduce()

The reduce method reduces the collection to a single value, passing the result of each iteration into the subsequent iteration:

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

const total = collection.reduce((carry, item) => carry + item);

// 6

The value for carry on the first iteration is null; however, you may specify its initial value by passing a second argument to reduce:

const total = collection.reduce((carry, item) => carry + item, 4);

// 10

reject()

The reject method filters the collection using the given callback. The callback should return true if the item should be removed from the resulting collection:

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

const filtered = collection.reject(value => value > 2);

// [1, 2]

For the inverse of the reject method, see the filter method.

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' },
// }

reverse()

The reverse method reverses the order of the collection's items:

const collection = collect([1, 2, 3, 4, 5]);

const reversed = collection.reverse();

reversed.all();

// [5, 4, 3, 2, 1]

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]

slice()

The slice method returns a slice of the collection starting at the given index:

const collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

const slice = collection.slice(4);

slice.all();

// [5, 6, 7, 8, 9, 10]

If you would like to limit the size of the returned slice, pass the desired size as the second argument to the method:

const slice = collection.slice(4, 2);

slice.all();

// [5, 6]

sole()

The sole method returns the first element in the collection that passes a given truth test, but only if the truth test matches exactly one element:

collect([1, 2, 3, 4]).sole(item => item === 1);

// 1

If there are no elements in the collection that should be returned by the sole method, then an error will be thrown:

collect([1, 2, 3, 4]).sole(item => item > 4);

// Error ('Item not found.') is thrown.

If there are multiple elements in the collection that should be returned by the sole method, then an error will be thrown:

collect([1, 2, 3, 4]).sole();

// Error ('Multiple items found.') is thrown.

Like the firstOrFail method, you may also pass an attribute, operator, and value:

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 },
]);

collection.sole('product', '=', 'Desk');

// { product: 'Desk', price: 200, discounted: true }

some()

Alias for the contains method.

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.

splice()

The splice method removes and returns a slice of items starting at the specified index:

const collection = collect([1, 2, 3, 4, 5]);

const chunk = collection.splice(2);

chunk.all();

// [3, 4, 5]

collection.all();

// [1, 2]

You may pass a second argument to limit the size of the resulting chunk:

const collection = collect([1, 2, 3, 4, 5]);

const chunk = collection.splice(2, 1);

chunk.all();

// [3]

collection.all();

// [1, 2, 4, 5]

In addition, you can pass a third argument containing the new items to replace the items removed from the collection:

const collection = collect([1, 2, 3, 4, 5]);

const chunk = collection.splice(2, 1, [10, 11]);

chunk.all();

// [3]

collection.all();

// [1, 2, 10, 11, 4, 5]

split()

The split method breaks a collection into the given number of groups:

const collection = collect([1, 2, 3, 4, 5]);

const groups = collection.split(3);

// [[1, 2], [3, 4], [5]]

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.

tap()

The tap method passes the collection to the given callback, allowing you to "tap" into the collection at a specific point and do something with the items while not affecting the collection itself:

collect([2, 4, 3, 1, 5])
  .sort()
  .tap((collection) => {
    console.log(collection.all());

    // [1, 2, 3, 4, 5]
  })
  .shift();

// 1

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"}

transform()

The transform method iterates over the collection and calls the given callback with each item in the collection. The items in the collection will be replaced by the values returned by the callback:

const collection = collect([1, 2, 3, 4, 5]);

collection.transform((item, key) => item * 2);

collection.all();

// [2, 4, 6, 8, 10]

Unlike most other collection methods, transform modifies the collection itself. If you wish to create a new collection instead, use the map method.

undot()

The undot method expands a single-dimensional collection that uses "dot" notation into a multi-dimensional collection:

const person = collect({
  'name.first_name': 'Marie',
  'name.last_name': 'Valentine',
  'address.line_1': '2992 Eagle Drive',
  'address.line_2': '',
  'address.suburb': 'Detroit',
  'address.state': 'MI',
  'address.postcode': '48219',
});

const undotted = person.undot();

const all = undotted.all();

// {
//   name: {
//       first_name: 'Marie',
//       last_name: 'Valentine',
//   },
//   address: {
//       line_1: '2992 Eagle Drive',
//       line_2: '',
//       suburb: 'Detroit',
//       state: 'MI',
//       postcode: '48219',
//   },
// }

union()

The union method adds the given array to the collection. If the given array contains keys that are already in the original collection, the original collection's values will be preferred:

const collection = collect({
  a: 'A',
  b: 'B',
});

const union = collection.union({
  a: 'AAA',
  c: 'CCC',
  b: 'BBB',
});

union.all();

// {
//   a: 'A',
//   b: 'B',
//   c: 'CCC',
// }

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

unwrap()

The unwrap method will unwrap the given collection:

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

collect().unwrap(collection);

// [1, 2, 3]

values()

The values method returns a new collection with the keys reset to consecutive integers:

const collection = collect({
  a: 'xoxo',
  b: 'abab',
  c: '1337',
  1337: 12,
});

const values = collection.values();

values.all();

// [12, 'xoxo', 'abab', '1337']

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 },
// ]

whereBetween()

The whereBetween method filters the collection within a given range:

const collection = collect([
  { product: 'Desk', price: 200 },
  { product: 'Chair', price: 80 },
  { product: 'Bookcase', price: 150 },
  { product: 'Pencil', price: 30 },
  { product: 'Door', price: 100 },
]);

const filtered = collection.whereBetween('price', [100, 200]);

filtered.all();

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

whereIn()

The whereIn method filters the collection by a given key / value contained within the given array.

const collection = collect([
  { product: 'Desk', price: 200 },
  { product: 'Chair', price: 100 },
  { product: 'Bookcase', price: 150 },
  { product: 'Door', price: 100 },
]);

const filtered = collection.whereIn('price', [100, 150]);

filtered.all();

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

When working with nested objects whereIn() method allows dot notated keys. E.g. whereIn('product.categories', ['office-supplies', 'furniture'])

whereInstanceOf()

The whereInstanceOf method filters the collection by a given class type:

const collection = collect([
  new Player('Firmino'),
  new Player('Salah'),
  new Manager('Klopp'),
]);

const filtered = collection.whereInstanceOf(Player);

filtered.all();

// [
//   new Player('Firmino'),
//   new Player('Salah'),
// ]

whereNotBetween()

The whereNotBetween method filters the collection within a given range:

const collection = collect([
  { product: 'Desk', price: 200 },
  { product: 'Chair', price: 80 },
  { product: 'Bookcase', price: 150 },
  { product: 'Pencil', price: 30 },
  { product: 'Door', price: 100 },
]);

const filtered = collection.whereNotBetween('price', [100, 200]);

filtered.all();

// [
//   { product: 'Chair', price: 80 },
//   { product: 'Pencil', price: 30 },
// ]

whereNotIn()

The whereNotIn method filters the collection by a given key / value not contained within the given array:

const collection = collect([
  { product: 'Desk', price: 200 },
  { product: 'Chair', price: 100 },
  { product: 'Bookcase', price: 150 },
  { product: 'Door', price: 100 },
]);

const filtered = collection.whereNotIn('price', [150, 200]);

filtered.all();

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

When working with nested objects whereNotIn() method allows dot notated keys. E.g. whereNotIn('product .categories', ['office-supplies', 'furniture'])

whereNotNull()

The whereNotNull method filters items where the given key is not null.

const collection = collect([{
  name: 'Mohamed Salah',
}, {
  name: null,
}, {
  name: 'Darwin NĂșñez',
}]);

const filtered = collection.whereNotNull();

filtered.all();

// [
//   { name: 'Mohamed Salah' },
//   { name: 'Darwin NĂșñez' },
// ]

whereNull()

The whereNull method filters items where the given key is null.

const collection = collect([{
  name: 'Mohamed Salah',
}, {
  name: null,
}, {
  name: 'Darwin NĂșñez',
}]);

const filtered = collection.whereNull();

filtered.all();

// [
//   { name: null },
// ]

wrap()

The wrap method will wrap the given value in a collection:

const collection = collect().wrap([1, 2, 3]);

collection.all();

// [1, 2, 3]

zip()

The zip method merges together the values of the given array with the values of the original collection at the corresponding index:

const collection = collect(['Chair', 'Desk']);

const zipped = collection.zip([100, 200]);

zipped.all();

// [['Chair', 100], ['Desk', 200]]

Contribute

PRs are welcomed to this project, and help is needed in order to keep up with the changes of Laravel Collections. If you want to improve the collection library, add functionality or improve the docs please feel free to submit a PR.

License

MIT © Daniel Eckermann

collect.js's People

Contributors

ajaymathur avatar anupvarghese avatar brainmaestro avatar dalook avatar dducro avatar dogoku avatar dsymonov-jc avatar ecrmnn avatar ennetech avatar g-plane avatar helfull avatar jbtbnl avatar jeromedeleon avatar lucacri avatar maicol07 avatar marcus-at-localhost avatar matrunchyk avatar mattstypa avatar nhat-phan avatar okonomiyaki3000 avatar pf-jordy avatar philippkuehn avatar s-patompong avatar sacharbit avatar slowvoid avatar stevebauman avatar tenghuanhe avatar v-stein avatar wangzhenhui1992 avatar zpwparsons avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

collect.js's Issues

how about adding a CONTRIBUTING.md?

Hey,

could you write a short CONTRIBUTING.md, so everyone who wants to constribute knows how.

i myself would like to constribute but i cant get the env working to run those tests.

mapWithKeys after groupBy errors with "this.items.forEach is not a function"

hey,

i have this pipeline

const stats = collect(this.content)
                    .filter(value => {
                        return collect([
                            'created', 'updated', 'deleted'
                        ]).contains(value.description);
                    })
                    .groupBy(value => value.description)
                    .mapWithKeys(value => {
                        return [value.description, collect(value.all()).count()];
                    });

and this does error with this.items.forEach is not a function at the mapWithKeys method,

is it intentional that mapWithKeys does not support hashmaps?

Working with objects

Just checking about the "wrapper for working with arrays and objects". Is this implemented yet? It's just when I pass in an object and try and use many of the functions: transform, map, pop etc. then it fails as it is trying to call the corresponding methods on this.items assuming that it is an array.

Am I doing something wrong or can I just not use objects in collect.js?

Brilliant library otherwise 👍

FlatMap functionality is not as expected

The flatMap method doesn't work as expected for a collection (an array of objects).
For example, if I have a list of employees and each employee has a list of customers and I want to get a list of all customer I would use flatMap as the below:
Input:

let employees = collect([
  {
    customers :[
      {
        name : "c1"
      },
      {
        name : "c2"
      }
    ]
  },
  {
    customers :[
      {
        name : "c3"
      }
    ]
  }
])

let customers = employees.flatMap(employee => employee.customers)

Expected result:

[
  {
    name : "c1"
  },
  {
    name : "c2"
  },
  {
    name : "c3"
  }
]

Actual result:
Error: Cannot read property '0' of undefined(in the collect.js flatMap source code)

Laravel's flatMap source code

Is there a good reason why flatMap behaves like that? If it is an issue with this can you, please, implement the correct version for flatMap?

Thank you!

`macro` method is only callable on an instance

Hey,

if i want to create a macro for global use, like in laravel. I would do something like this:

collect.marco('hello', function(fn) {
  console.log('hello');
});

and then use it were ever i need it.
but with the current implementation i get the error:

collect.macro is not a function

How to use without npm?

Hello

Is it possible to use this package without npm?

I was expecting to see a collect.js or a collect.min.js in the dist folder.

How to access value by index ID in array ?

*Library is missing Value By Index Functionality

ex

myArray = collect([1,4,5,9,9,5,1,1,5,4,1,7,1,0,9,3,7,4,8,9,3,6,3,3]);

val = myArray[3];

it is not returning value .

it should return value 9

Cast object to array

Would it be possible to auto-cast the collection to an array like Laravel does when PHP expects an array? For example this isn't possible right now:

const collection = collect([1,2,3,4,5]);

for (let item of collection) {
    // Do something with item
}

Suggestion: Offer the not implemented methods as aliases?

I was looking at the few methods that are not implemented, but which are in Laravel, and was wondering if it might be a good idea to implemented them as aliases/wrappers that just call the intended method? This would keep users from having to worry about differences to Laravel, since overall it is expected to be the same (noting that this collection manager is always strict).

ReferenceError: collect is not defined

Hi

I am using Laravel 5.5;

I have installed using npm

npm install --save collect.js

and added the following on bootstrap.js

window.collect = require('collect.js')

and run
npm run dev

inside my blade blade file i have added

<script>
collect([1, 2, 3]).all();
</script>

But I am getting an error

ReferenceError: collect is not defined

toArray - cannot reproduce the following Laravel Collection behavior

$collection = new Collection([
		['shape' => 'square', 'color' => 'blue', 'val' => 'blue square'],
		['shape' => 'square', 'color' => 'blue', 'val' => 'blue square'],
		['shape' => 'triangle', 'color' => 'green', 'val' => 'green triangle'],
		['shape' => 'triangle', 'color' => 'red', 'val' => 'red triangle'],
]);

$result = $collection->groupBy('shape')->map(
	function(Collection $dimension1Group) {
		return $dimension1Group->groupBy('color');
	}
)->toArray();

//array (
//	'square' =>
//		array (
//			'blue' =>
//				array (
//					0 =>
//						array (
//							'shape' => 'square',
//							'color' => 'blue',
//							'val' => 'blue square',
//						),
//					1 =>
//						array (
//							'shape' => 'square',
//							'color' => 'blue',
//							'val' => 'blue square',
//						),
//				),
//		),
//	'triangle' =>
//		array (
//			'green' =>
//				array (
//					0 =>
//						array (
//							'shape' => 'triangle',
//							'color' => 'green',
//							'val' => 'green triangle',
//						),
//				),
//			'red' =>
//				array (
//					0 =>
//						array (
//							'shape' => 'triangle',
//							'color' => 'red',
//							'val' => 'red triangle',
//						),
//				),
//		),
//)

I'd like to address two things with this example:

  • The collect().toArray function does not support iteration over nested collections when the topmost collection is a key-value map (javascript object).
  • We lose the keys when using collect().toArray as opposed to Laravel Collection. In case this is the expected behavior (after all Javascript != PHP), it would be helpful to specify this deviation in the README.

pull leaving behind undefined objects in collection

const testCollection = collect(["test-1", "test-2", "test-3", "test-4", "test-5"]);

const test1 = testCollection.pull(0);
console.log(test1);
console.log(testCollection.count());

const test2 = testCollection.pull(1);
console.log(test2);
console.log(testCollection.count());

testCollection.push("test-6");
console.log(testCollection.count());

testCollection.push("test-7");
console.log(testCollection.count());

console.log(testCollection.all());

Results:
image

Regression stats / benchmark

Thanks for your work on this. Do you have any stats on upper limits with larger collection sizes, benchmarked performances etc?

it is possible to get collection async?

i want something like

const collect = require('collect.js')

collect(['a', 'b', 'c'])
    .toArray(array => /** do something */)

it is possible to achieve with current version?

bug in merge&union method?

`const collection = collect({
a: 'A',
b: 'B',
d: 'D'
});

const merged = collection.merge({
a: 'AAA',
b: 'BBB',
c: 'CCC'
});

console.log(merged.all()) // { a: 'AAA', b: 'BBB', c: 'CCC' } but expect { a: 'AAA', b: 'BBB', c: 'CCC' , d: 'D'}

console.log(merged.all().b) // BBB
merged.forget('b')
console.log(merged.all().b) // B, expect undefined
i know in method file: merge.js and union.js usevar collection = Object.create(this.items), does use clone` like is right/better?

env: node -v v6.10.2

how to use it in plain javascript ?

Hi i want to use this library in plain javascript like we use Jquery.

this is very good library and i want to use this in my html file.
please guide how to use this.

Splitting functions into separate files

Due to the number of implemented functions, the code starts getting a little difficult to parse. Would you be interested in splitting the functions into separate files? It's not necessary, but I feel like it would help encourage contribution.

Great project btw.

Most methods only work if underlying data is an array

Most methods (like "where") only work on array data.

Take this as an example:

let filtered = collect([
    { test: 1 },
    { test: 2 }
]).where('test', 2);

filtered equals a collection with just the second object.
However the following throws an exception.

let filtered = collect([
    { test: 1 },
    { test: 2 }
]).keyBy('test').where('test', 2);

This is because by calling keyBy() the underlying array is converted to an object.

I understand this is done, however I also expect methods like where() are still usable.

Is it an option to convert the items property to an array (if it is an object) in the affected methods to preserve expected behavior?
Beside this keys also need the be preserved, so just converting it to an array is not a complete solution..

Unable to run npm run production in laravel 5.4

I'm importing collect globally in laravel's default bootstrap.js file:

window.collect = require('collect.js');

When I run npm run development everything works fine.

When I run npm run production I get the following error:

> @ production C:\www\kilt2016
> cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js

 95% emitting ERROR  Failed to compile with 1 errors7:21:22 PM

 error

/js/oa/vue.57617082aa194c799028.js from UglifyJs
Unexpected token: name (index) [./~/collect.js/src/methods/chunk.js:5,0][/js/oa/vue.57617082aa194c799028.js:79,6]

(Laravel version: 5.4.24, Mix Version: 0.12.1)

Anyone any ideas?

random() mutates the collection

The random method returns a random item from the collection:

Not only that, it shuffles the collection in place:

const a = collect([1, 2, 3, 4, 5])

console.log(a.random())    // 4
console.log(a.all())       // [3, 5, 1, 4, 2]

I humbly suggest it would be more clear to either update the documentation to note that, or to adjust the code to be pure -- a user might not expect their ordered array to be shuffled when asking for a random element.

Sorting strings with accented characters

Knows how to order an array with words with special characters like accents?

Javascript has a native method sort that allows sorting ( array.sort() ). Also you can provide your own custom sorting function. Are two ways to overcome this behavior localeCompare and Intl.Collator provided. in .sortBy() could have this option.

collect(array).sortBy('name');

returns 'Albacete' before 'Álava', and I want 'Álava' before 'Albacete'. =(

Thanks a lot, @ecrmnn

Firefox SyntaxError: missing = in const declaration

i'm getting this error on firefox

SyntaxError: missing = in const declaration

module.exports = function except(properties) {
const collection = {};

for (const prop in this.items) {
if (properties.indexOf(prop) === -1) {
collection[prop] = this.items[prop];
}
}

return new this.constructor(collection);
};

What does it should be when we call `toArray()`?

Suppose there is an object collection like that

people = {
  name: 'wei',
  sex: 'male',
  phone: [15211111111, 13511111111],
  company: {
    name: 'IBM',
    city: 'BJ'
  }
}

What does it should be if we call collect(people).toArray()?
In current implementation, it will return
[ 'wei', 'male', [ 15211111111, 13511111111 ], { name: 'IBM', city: 'BJ' } ].
Should it be [ 'wei', 'male', [ 15211111111, 13511111111 ], [ 'IBM', 'BJ' ] ]?

can't setup collect.js in vue js

NB: SOLVED

<template lang="html">
  <div class="app">
    {{ test}}
  </div>
</template>

<script>

import collect from  'collect.js'

export default {
data () {
  return {
    msg: 'Atom_Vue',
    test: ' '
  }
},
created() {
    this.test = collect([1, 2, 3, 4]).count()
  },
}
</script>

<style lang="css" scoped >

</style>

output issue :

Error in render: "TypeError: [1,2,3,4].count is not a function"

.all, toArray, toJSON not returning expected results

If I use those three methods(.all, toArray, toJSON) on a collection with sub-collections I get the items property from children in the result.

It shouldn't recursively get the items for each collection?

Why I have sub-collections? Because I want, at some point, to process only the data from children and I don't want to create temporary variables and instantiate a new collection().

Sum using iterator/reduce

I've opened PR#95 several days ago, which uses reduce instead of iterator to implement sum function with fewer lines of code. However, a simple benchmark test shows iterator_sum is 3x ~ 5x times faster than reduce_sum.

According to https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce, reduce does have extra overhead compared to directly using iterator.

So the change of PR#95 may be revoked and using origin sum implementation is better. Using reduce makes the code shorter, it harms efficiency. How do you think about it? @ecrmnn

Doesn't work with vuejs v-for

First thanks for this package, it's like using laravel collections.

I tried to use one of your collections in a v-for in vue and it doesn't iterate over the collection items, instead it iterates over the collection Object properties.

I have the latest version of your package.

where(' ' ,'like', ' ') like operator

This is awesome collection as like laravel .
like operator is very important as query system.
.where('name','like','s%')
another is
.orWhere()

.where('votes', '>', 100)
.orWhere('name', 'John')

andWhere() array condition

$users = DB::table('users')->where([
    ['status', '=', '1'],
    ['subscribed', '<>', '1'],
])->get();

without those can't feel happiness .please add those collections. @ecrmnn ❀

count() does not work with hashmaps

hey,

counting objects in a hashmap returns undefined

collect({}).count() // undefined
collect({foo:'bar'}).count() // undefined

might just be a version problem here

Contains on collection of objects

The contains function isn't working properly on collections of objects.

Example:

let collection = collect([{
  name: 'Steven Gerrard',
  number: 8
}, {
  name: 'Steve Jobs',
  number: 2
}]);

let result = collection.contains('name', 'Steven Gerrard'); // false
// Expected result: true

For people having the same issue, you can currently use the following as a workaround:

collection.where('name', 'Tony Stark').first() === undefined;

Merge() not working as expected ?

Hey,

I'm noticing an issue with the merge() method. Here's what I'm getting running the example:

const collection = collect({
	id: 1,
	price: 29
});

const merged = collection.merge({
	price: 400,
	discount: false
});

console.log(merged.all());

In my console:

img

So, I don't have a complete understanding of JS prototypes, but from what I can see the object returned only has the content of the merged object (the second object), and it's prototype has the original data (the first object).

Am I not getting something or is this unexpected behavior ?

Thanks !

The only() method with array It does not work properly

const collection = collect([{
  name: 'JavaScript: The Good Parts', pages: 176
}, {
  name: 'JavaScript: The Definitive Guide', pages: 1096
}]);

return collection.only('name').all()

I'm trying to find a 'name' property from the collection. It does not work properly. It displays empty arrays [] I want to know my approach is correct or not.

Thank you.

except on arrays

Problem:
except doesn't work on arrays, but intersect does? Is this inconsistent or correct behavior?

const testE = collect([1,2,3,4,5]).except([2,12]).all()
// result is an object: ​​​​​{ '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }​​​​​
// think i wanted array: [12]

const testI = collect([1, 2, 3, 4, 5]).intersect([1, 2, 3, 9]).all();
// result: [1,2,3]

except returns object, intersect returns array.

in sql world:
http://www.essentialsql.com/wp-content/uploads/2014/10/UnionInsersectExcept.jpg

thanks :-)

Ps. Love this awesome framework ;-)

Adding dump command?

In light of the pending Laravel 5.5 release, so you have any plans for add dump and/or dd commands?

Doesn't work well in IE

I get a const is not initialized. It points to this function, but I guess it's valid for all. Would it be possible to make it available for IE too?

Collection.prototype.only = function (properties) {
  const collection = {};

  for (const prop in this.items) {
    if (properties.indexOf(prop) !== -1) {
      collection[prop] = this.items[prop];
    }
  }

  return new Collection(collection);
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.