-
Notifications
You must be signed in to change notification settings - Fork 0
Collection
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'
}
]
Returns all the records on the collection.
var result = collection.all();
// [ { id: 1, name: 'Alex', ... }, { id: 2, name: 'Tamara', ... }, { id: 3, name: 'Celia', ... } ]
Returns the average value of the given field value.
var result = collection.avg( 'money' );
// 10000
Check if collection has given property "key: value".
var result = collection.contains( 'name', 'Peter' );
// false
Return the number of records of the collection.
var result = collection.count();
// 3
Iterate over the collection passing a callback.
var result = collection.each( function( element, index )
{
} );
Note: if callback return false, iteration is stopped.
Return the first element of the collection.
var result = collection.first();
// { id: 1, name: 'Alex', ... }
Return the first element of the collection.
var result = collection.last();
// { id: 3, name: 'Celia', ... }
Return the max number in given field of the collection.
var result = collection.max( 'money' );
// 15000
Return the max date in given field of the collection. ( Works with native Date object )
var result = collection.max( 'birthdate' );
// 1991-08-29
Return the min number in given field of the collection.
var result = collection.min( 'money' );
// 5000
Return the min date in given field of the collection. ( Works with native Date object )
var result = collection.min( 'birthdate' );
// 1972-01-29
Return all the collection values for a given field.
var result = collection.pluck( 'name' );
// [ 'Alex', 'Tamara', 'Celia' ]
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', ... } ]
Returns a random item from the collection.
var result = collection.random();
// { id: 2, name: 'Tamara', ... } --> Random
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 );
// []
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', ... } ]
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', ... } ]
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', ... } ]
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', ... } ]
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 values of given field and return the total.
var result = collection.sum( 'money' );
// 30000
Return the collection data as JSON string.
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', ... } ]
Updates an object from the collection by given pair field-value.
collection.updateByField( 'name', 'Alex', 'Peter' );
collection.first();
// { id: 1, name: 'Peter', ... }