Skip to content

Collection

Aferz edited this page Feb 6, 2016 · 7 revisions

To handle your query records stupidly easy, Vivaz offers an outstanding Collection object. (Based on the awesome Laravel Collection Class)

Note: All methods listed below support dot notation.

Example data:

var data = [
    { 
        id: 1,
        name: 'Alex',
        car: {
            brand: 'Hyundai',
            model: 'i30'
        },
        money: '5000',
        birhtdate: '1991-08-29'
    }, 
    { 
        id: 2,
        name: 'Tamara',
        car: {
            brand: 'Audi',
            model: 'A1'
        },
        money: '10000',
        birhtdate: '1985-02-02'
    },
    { 
        id: 3,
        name: 'Celia',
        car: {
            brand: 'Audi',
            model: 'A3'
        },
        money: '15000',
        birhtdate: '1972-01-29'
    }
]

.all()

Returns all the records on the collection.

var result = collection.all();

// [ { id: 1, name: 'Alex', ... }, { id: 2, name: 'Tamara', ... }, { id: 3, name: 'Celia', ... } ]

.avg()

Returns the average value of the given field value.

var result = collection.avg( 'money' );

// 10000

.contains()

Check if collection has given property "key: value".

var result = collection.contains( 'name', 'Peter' );

// false

.count()

Return the number of records of the collection.

var result = collection.count();

// 3

.each()

Iterate over the collection passing a callback.

var result = collection.each( function( element, index )
{

} );

Note: if callback return false, iteration is stopped.

.first()

Return the first element of the collection.

var result = collection.first();

// { id: 1, name: 'Alex', ... }

.last()

Return the first element of the collection.

var result = collection.last();

// { id: 3, name: 'Celia', ... }

.max()

Return the max number in given field of the collection.

var result = collection.max( 'money' );

// 15000

.maxDate()

Return the max date in given field of the collection. ( Works with native Date object )

var result = collection.max( 'birthdate' );

// 1991-08-29

.min()

Return the min number in given field of the collection.

var result = collection.min( 'money' );

// 5000

.minDate()

Return the min date in given field of the collection. ( Works with native Date object )

var result = collection.min( 'birthdate' );

// 1972-01-29

.pluck()

Return all the collection values for a given field.

var result = collection.pluck( 'name' );

// [ 'Alex', 'Tamara', 'Celia' ]

.pop()

Remove and return the last element of the collection

var result = collection.pop();

// { id: 3, name: 'Celia', ... }

collection.all()

// [ { id: 1, name: 'Alex', ... }, { id: 2, name: 'Tamara', ... } ]

.random()

Returns a random item from the collection.

var result = collection.random();

// { id: 2, name: 'Tamara', ... } --> Random

.search()

Search a value for a given field in the collection and return the indexes of the objects.

var result = collection.search( 'car.model', 'i30' );

// [ 1 ]

A third optional parameter can be passed as true for a strict comparison.

var result = collection.search( 'id', '1', true );

// []

.shift()

Remove and return the first element of the collection

var result = collection.shift();

// { id: 1, name: 'Alex', ... }

collection.all()

// [ { id: 2, name: 'Tamara', ... }, { id: 3, name: 'Celia', ... } ]

.sort()

Order the collection by given field. If no order given as second parameter ('asc' or 'desc'), ascendent by default.

var result = collection.sort( 'name' );

// [ { id: 1, name: 'Alex', ... }, { id: 3, name: 'Celia', ... }, { id: 2, name: 'Tamara', ... } ]

.sortDesc()

Order the collection by given field in descendant order. Alias of sort( field, 'desc' ).

var result = collection.sortDesc( 'name' );

// [ { id: 2, name: 'Tamara', ... }, { id: 3, name: 'Celia', ... }, { id: 1, name: 'Alex', ... } ]

.splice()

Removes an return an array of elements by given index from the collection. Can be passed a number or an array of numbers.

var result = collection.splice( 1 );

// [ { id: 2, name: 'Tamara', ... } ]

collection.all()

// [ { id: 1, name: 'Alex', ... }, { id: 3, name: 'Celia', ... } ]

.spliceByValue()

Removes an return an array of elements by given pair field-value from the collection.

var result = collection.spliceByValue( 'car.brand', 'Audi' );

// [ { id: 2, name: 'Tamara', ... }, { id: 3, name: 'Celia', ... } ]

collection.all()

// [ { id: 1, name: 'Alex', ... } ]

Optionally, you can pass a third argument as true for strict comparison.

var result = collection.spliceByValue( 'id', '1', true );

// []

.sum()

Sum values of given field and return the total.

var result = collection.sum( 'money' );

// 30000

.toJson()

Return the collection data as JSON string.

.update()

Updates an object from the collection by given index. Can be passed a number or an array of numbers.

collection.update( 0, 'name', 'Peter' );

collection.first();

// { id: 1, name: 'Peter', ... }
collection.update( [ 1, 2 ], 'name', 'Penny' );

collection.all();

// [ { id: 1, name: 'Alex', ... }, { id: 2, name: 'Penny', ... }, { id: 3, name: 'Penny', ... } ]

.updateByField()

Updates an object from the collection by given pair field-value.

collection.updateByField( 'name', 'Alex', 'Peter' );

collection.first();

// { id: 1, name: 'Peter', ... }