Class o2.Collection
static
class
o2.Collection
An alias to o2.Collection.some.
Removes all the elements of the Object
.
Usage example:
var obj = {a:'b'}; o2.Collection.clear(obj); // obj is now {} obj = [1,2,3]; o2.CollectionHelper.clear(obj); // obj is now []
An alias to o2.Collection.copy.
Usage example:
Remove null
, and undefined
members from
the Object
.
This function alters the actual Object
.
Usage example:
var ar = [null, 1, 2, undefined, '', 3, 4]; o2.Collection.compact(ar); // ar is now [1, 2, '', 3, 4]
An alias to o2.Collection.indexOf(ar,
elm) > -1
.
Usage example:
var ar = [1, 2, 3, 4]; var isInAr = o2.Collection.contains(ar, 3); // isInAr is true
Creates a clone of the given Object
, and returns it;
leaving the original intact.
Usage example:
var obj1 = {a:'b'}; var obj2 = o2.Collection.copy(obj1);
An alias to o2.Collection.find.
Usage example:
Takes the difference between the current collection and a number of other collections. Only items that do not remain in the rest of the collections will be returned.
Usage example:
var ar1 = [1, 2, 3, 4, 5]; var ar2 = [2, 3, 4, 5, 6]; var ar3 = o2.Collection.diff(ar1, ar2); // ar3 is [1, 5. 6]
An alias to o2.Collection.forEach.
Check whether every element of a collection passes a truth test.
Usage example:
var collection = [1, 2, 3, 4, 5]; var isAllNumeric = o2.Collection.every(collection, function(item) { return !isNaN(item); }); // isAllNumeric will be true
Excludes filtered out items from the collection. Returns a new collection without alterin the initial one.
Usage example:
var collection = [1, 2, 3, 4, 5]; var rest = o2.Collection.exclude(collection, function(item) { return item % 2 === 0; }); // rest will be [1, 3, 5]
Merges two Object
s or Array
s.
Usage example:
var base = {lorem: 'ipsum'}; var child = {dolor: 'sit'}; o2.Collection.extend(child, base); // child => {lorem : 'ipsum', dolor : 'sit'}
An alias to o2.Collection.grep.
Gets the first collection item that validates against the given delegator.
Usage example:
var ar = [1, 2, 3, 4]; var res = o2.Collection.find(ar, function(value){ return value === 3; }); // res will be 3
Shallow flattens an Array
.
Does not alter the original object, returns a new flattened object instead.
Usage example:
var ar = [[1, 2], [3, 4], 5] var flattened = o2.Object.flatten(ar); // flattened is [1, 2, 3, 4, 5]
An alias to {o2.Collection.reduce}.
An alias to o2.Collection.reduceRight.
Executes a delegate of the form
fn(item, currentIndex, collection)
for each element
of the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]; o2.Collection.forEach(collection, function(item, index, collection) { log(item); }); // will log: 1 2 3 4
An alias to {o2.Collection.getSize}
An alias to o2.Collection.diff.
Gets the first item in the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]
Gets the first n elements of the collection.
Usage example:
var collection = [1, 2, 3, 4, 5, 6]; var firstFew = o2.Collection.getFirstN(collection, 3); // firstFew will be [1, 2, 3]
Gets all the static methods of the object.
Usage example:
var obj = {fn1 : function(){}, fn2 : function() {}, lorem : 1}; var methods = o2.Collection.getFunctions(obj); // methods now is [fn1, fn2]
Gets all the keys of the object.
Usage example:
var obj = {lorem : 'ipsum', dolor : 'sit'}; var keys = o2.Collection.getKeys(obj); // keys will be ['lorem', 'dolor']
Gets the last item in the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]; var last = o2.Collection.getLast(collection); // last will be 5
Gets the last n items in the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]; var lastFew = o2.Collection.getLastN(collection, 3); // lastFew will be [3, 4, 5]
An alias to {o2.Collection.getSize}
Gets the maximum value of the collection.
Usage example:
var collection = [1, 3, 11, 42, 4, 5, 6]; var meaningOfLife = o2.Collection.getMax(collection); // meaningOfLife will be 42 var meaningOfUniverse = o2.Collection.getMax(collection, function(item) { return item !== 42 ? Math.PI : 42; }); // meaningOfUniverse will also be 42
An alias to o2.Collection.getFunctions.
Gets the maximum value of the collection.
Usage example:
var collection = [111, 311, 1211, 42, 114, 235, 126]; var meaningOfLife = o2.Collection.getMin(collection); // meaningOfLife will be 42 var meaningOfUniverse = o2.Collection.getMin(collection, function(item) { return item !== 42 ? 42 * Math.PI : 42; }); // meaningOfUniverse will also be 42
Gets the elements of the collection after index n.
Usage example:
var collection = [1, 2, 3, 4, 5]; var rest = o2.Collection.getRest(collection); // rest will be [2, 3, 4, 5] rest = o2.Collection.getRest(collection, 2); // rest will be [3, 4, 5];
Gets the number of items in the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]; var size = o2.Collection.getSize(collection); // size will be 5
Gets an index to insert the item at a sorted Array
,
so that is not needed to be resorted.
Usage example:
var ar = [1, 2, 24, 30, 44, 66, 100]; var idx = o2.Collection.getSortedIndex(42); // idx will be 4
Gets the value of an Object
that has
{key1 : value1, key2 : value2 ... }
kind of layout.
If an Array
is passed, it makes a shallow copy of that
array and returns it.
Usage example:
var collection = {lorem : 1, ipsum : 2, dolor : 3}; var values = o2.Collection.getValues(collection); // values will be ['lorem', 'ipsum', 'dolor']
Filters the items of a collections using an evaluator delegate and returns the filtered result set.
Usage example:
var collection = [1, 2, 3, 4, 5, 6]; var evens = o2.Collection.grep(collection, function(item) { return item % 2 === 0; }); // evens will be [2, 4, 6]
Groups the items in the collection by a key or an evaluator
Function
.
Usage example:
var collection = [ {lorem : 1}, {lorem : 2}, {lorem : 3}, {ipsum : 1}, {ipsum : 2} ]; var lorems = o2.Collection.group(collection, 'lorem') //lorems will be {lorem : [1, 2, 3]} var grouped = o2.Collection.group(collection, function(item) { for(key in item) { if (item.hasOwnProperty(key)) { return key; } } }); // grouped will be {lorem : [1, 2, 3], ipsum : [1, 2]}
An alias to o2.Collection.contains
Usage example:
An alias to o2.Collection.contains
Gets the index of the element in the given Array
.
Usage example:
var ar [1, 2, 3, 4]; var id = o2.Collection.indexOf(ar, 3); // id is 2
Returns an Array
of items that are common in all of
the collections passed in as parameters.
Usage example:
var ar1 = [1, 2, 3, 4, 5]; var ar2 = [2, 3, 4, 5, 6]; var ar3 = o2.Collection.intersect(ar1, ar2); // ar3 is [2, 3, 4, 5]
Calls the delegate Function
with an optional set
of parametrs for each item in the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]; function log(item) { console.log(item); } o2.Collection.invoke(collection, log); // will log: // 1 // 2 // 3 // 4 // 5
Check whether the collection contains any members.
Usage example:
// This will return true: o2.Collection.isEmpty([]); // This will also return true: o2.Collection.isEmpty({});
Returns the last index of the given item.
Usage example:
var collection = [1, 2, 4, 2, 42, 2, 4, 42, 21, 12, 1]; var idx = o2.Collection.lastIndexOf(collection, 42); // idx will be 7
Calls a Function
for each member of the collection,
passing the current item as a parameter. Returns an Array
containing the results of each call.
Usage example:
var collection = [1, 2, 3, 4, 5]; var squared = o2.Collection.map(collection, function(item) { return item*item; }); // squared will be [1, 4, 9, 25]
An alias to o2.Collection.extend.
Hard to explain in words. Let us demonstrate by an example:
Usage example:
var collection = [ {key1 : {lorem1 : 'ipsum1'}, key2 : {dolor1 : 'amet1'}}, {key1 : {lorem2 : 'ipsum2'}, key2 : {dolor2 : 'amet2'}}, {key1 : {lorem3 : 'ipsum3'}, key2 : {dolor3 : 'amet3'}} ]; // Will return: // [ // {dolor1 : 'amet1'}, // {dolor2 : 'amet2'}, // {dolor3 : 'amet3'} // ] o2.Collection.pluck(collection, 'key2');
Works similar to the reduce part of the Map Reduce algorithm.
Reduces a collection into a single value by
applying a delegate of the form
function(cache, value, index, collection)
where
cache is the accumulator, value
is the iterated item, index is the item's index,
and collection is the collection we are working on.
Usage example:
var collection = [1, 2, 3, 4, 5]; var reduced = o2.Collection.reduce(collection, function(store, value) { return store + value; }, 0); // reduced will be 15
Works similar to o2.Collection.fold, but goes from the end of the collection to the beginning of the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]; var reduced = o2.Collection.reduceRight(collection, function( store, value) { return store + value; }, 0); // reduced will be 15
An alıas to o2.Collection.exclude.
Removes all ocurences of the element from the collection.
Usage example:
var collection = [1, 2, 3, 4, 5] var result = o2.Collection.removeElement(collection, 3); // result will be [1, 2, 4, 5]
Removes and element from the collection if it has a property named name with a value value.
This method works by reference, and alters the given collection.
Usage example:
var collection = { lorem : 1, ipsum : 2, sit : 3} o2.Collection.removeElementByValue(collection, 'sit', 3); // collection will be {lorem : 1, ipsum : 2}
An alias to o2.Collection.grep.
Randomizes the collection. Does not alter the original collection, just returns a randomized copy.
Usage example:
var ar = [1, 2, 3, 4, 5]; var shuffled = o2.Collection.shuffle(ar);
Checks whether at least one element of the given collection satisfies a condition given with the delegate.
The delegate is in the form
function(context, value, index, collection)
, iterates
through the items of the collection and returns
a boolean value. When this delegate
returns true
in any iteratioin, some(...)
also returns true; it returns false
otherwise.
Usage example:
var collection = [1, 2, 3, 4, 5]; var isSome = o2.Collection.some(collection, function(item) { return item > 4; }); // isSome will be true
Sorts the collection.
Contrary to Array.prototype.sort
, this function does not
sort the collection in place, and therefore it does not alter the
initial object's contents.
Usage example:
var collection = [1, 2, 5, 4, 7]; var sorted = o2.Collection.sort(collection, function(item) { return item; }); // sorted will be [1, 2, 4, 5, 7]
Safely converts the Object
in question into anarray.
Usage example:
var obj = {lorem : 1, ipsum : 2}; var ar = o2.Collection.toArray(obj); // ar will be [1, 2]
Merges several collections into a single Array
Usage example:
var union = o2.Collection.union([1, 2], [2, 3], [3, 4], [5]); // union will be [1, 2, 3, 4, 5]
Removes duplicate entries from the collection. Returns a new
Array
; does not alter the original collection.
Usage example:
var ar = [1, 2, 3, 2, 4, 2, 42]; var uniq = o2.Collection.unique(ar); // uniq will be [1, 2, 3, 4, 42]
Takes a set of Array
s as parameters and brings together
the elements that have the same index.
Usage example:
var ar1 = [1,2,3]; var ar2 = ['a', 'b', 'c', 'd']; var ar3 = [true, false]; // returns: // [ // [1, 'a', true], // [2. 'b', false], // [3, 'c'], // ['d'] // ] zip(ar1, ar2, ar3);
Function Details
function any
static
any()
An alias to o2.Collection.some.
function clear
static
clear(Object
ar)
Removes all the elements of the Object
.
Usage example:
var obj = {a:'b'}; o2.Collection.clear(obj); // obj is now {} obj = [1,2,3]; o2.CollectionHelper.clear(obj); // obj is now []
ar
- the Object
to clear. function clone
static
clone()
An alias to o2.Collection.copy.
Usage example:
function compact
static
compact(Object
ar)
Remove null
, and undefined
members from
the Object
.
This function alters the actual Object
.
Usage example:
var ar = [null, 1, 2, undefined, '', 3, 4]; o2.Collection.compact(ar); // ar is now [1, 2, '', 3, 4]
ar
- the Object
to clean. Object
itself.
function contains
static
contains(Array
ar, Object
elm)
An alias to o2.Collection.indexOf(ar,
elm) > -1
.
Usage example:
var ar = [1, 2, 3, 4]; var isInAr = o2.Collection.contains(ar, 3); // isInAr is true
ar
- the Array
to search.
elm
- the Object
to match. true
if the Array
contains the item,
false
otherwise.
function copy
static
copy(Object
ar)
Creates a clone of the given Object
, and returns it;
leaving the original intact.
Usage example:
var obj1 = {a:'b'}; var obj2 = o2.Collection.copy(obj1);
ar
- the object to clone. Object
.
function detect
static
detect()
An alias to o2.Collection.find.
Usage example:
function diff
static
diff(Arguments
...)
Takes the difference between the current collection and a number of other collections. Only items that do not remain in the rest of the collections will be returned.
Usage example:
var ar1 = [1, 2, 3, 4, 5]; var ar2 = [2, 3, 4, 5, 6]; var ar3 = o2.Collection.diff(ar1, ar2); // ar3 is [1, 5. 6]
...
- variable number of input arguments; each
argument should either be an Array
or an iterable
Object
. Array
of non-matching items.
function each
static
each()
An alias to o2.Collection.forEach.
function every
Check whether every element of a collection passes a truth test.
Usage example:
var collection = [1, 2, 3, 4, 5]; var isAllNumeric = o2.Collection.every(collection, function(item) { return !isNaN(item); }); // isAllNumeric will be true
obj
- an Array
or an iterable collection.
delegate
- an iterator of the form
function(item, index, obj)
; where item is
the current collection item, index is the current index
and obj is the collection itself.
context
- (optional, defaults to undefined
)
the context that the delegate
uses as the this
reference. true
if delegate returns
true
for every element of the collection; false
otherwise.
function exclude
Excludes filtered out items from the collection. Returns a new collection without alterin the initial one.
Usage example:
var collection = [1, 2, 3, 4, 5]; var rest = o2.Collection.exclude(collection, function(item) { return item % 2 === 0; }); // rest will be [1, 3, 5]
obj
- an Array
or an iterable
Object
to work on.
delegate
- the iterator in the form
function(context, value, index, obj)
where
value is the current element of obj
being iterated over, and index is the index of that
element.
context
- (optional, defaults to undefined
)
the context that the delegate
uses as the this
reference. function extend
Merges two Object
s or Array
s.
Usage example:
var base = {lorem: 'ipsum'}; var child = {dolor: 'sit'}; o2.Collection.extend(child, base); // child => {lorem : 'ipsum', dolor : 'sit'}
toObj
- the Object
to copy values to.
fromObj
- the Object
to copy values from. toObj
.
function filter
static
filter()
An alias to o2.Collection.grep.
function find
Gets the first collection item that validates against the given delegator.
Usage example:
var ar = [1, 2, 3, 4]; var res = o2.Collection.find(ar, function(value){ return value === 3; }); // res will be 3
obj
- the Array
or an iterable
Object
.
delegate
- Iterator Function
in the form
function(value, index, collection)
.
context
- (optional, defaults to undefined
)
the context that acts as the this
reference in the iterator. null
if nothing
is found.
function flatten
static
flatten(Object
obj)
Shallow flattens an Array
.
Does not alter the original object, returns a new flattened object instead.
Usage example:
var ar = [[1, 2], [3, 4], 5] var flattened = o2.Object.flatten(ar); // flattened is [1, 2, 3, 4, 5]
obj
- an Array
or an iterable
Object
to work on. function fold
static
fold()
An alias to {o2.Collection.reduce}.
function foldR
static
foldR()
An alias to o2.Collection.reduceRight.
function forEach
static
forEach(Object
obj, Function
delegate)
Executes a delegate of the form
fn(item, currentIndex, collection)
for each element
of the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]; o2.Collection.forEach(collection, function(item, index, collection) { log(item); }); // will log: 1 2 3 4
obj
- the Array
or an iterable
Object
.
delegate
- the iterator in the form
function(item, index, collection)
. function getCount
static
getCount()
An alias to {o2.Collection.getSize}
function getDifference
static
getDifference()
An alias to o2.Collection.diff.
function getFirst
static
getFirst(Object
obj)
Gets the first item in the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]
obj
- an Array
or an iterable
Object
to work on. null
otherwise.
function getFirstN
static
getFirstN(Object
obj, Integer
n)
Gets the first n elements of the collection.
Usage example:
var collection = [1, 2, 3, 4, 5, 6]; var firstFew = o2.Collection.getFirstN(collection, 3); // firstFew will be [1, 2, 3]
obj
- an Array
or an iterable
Object
to work on.
n
- the number of items to retrieve. function getFunctions
static
getFunctions(Object
obj)
Gets all the static methods of the object.
Usage example:
var obj = {fn1 : function(){}, fn2 : function() {}, lorem : 1}; var methods = o2.Collection.getFunctions(obj); // methods now is [fn1, fn2]
obj
- an Array
or an iterable
Object
to work on. Function
s in the current
object.
function getKeys
static
getKeys(Object
obj)
Gets all the keys of the object.
Usage example:
var obj = {lorem : 'ipsum', dolor : 'sit'}; var keys = o2.Collection.getKeys(obj); // keys will be ['lorem', 'dolor']
obj
- an Array
or an iterable
Object
to work on. Array
of the object's keys.
function getLast
static
getLast(Object
obj)
Gets the last item in the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]; var last = o2.Collection.getLast(collection); // last will be 5
obj
- an Array
or an iterable
Object
to work on. null
otherwise.
function getLastN
static
getLastN(Object
obj, Integer
n)
Gets the last n items in the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]; var lastFew = o2.Collection.getLastN(collection, 3); // lastFew will be [3, 4, 5]
obj
- an Array
or an iterable
Object
to work on.
n
- the number of items to retrieve. function getLength
static
getLength()
An alias to {o2.Collection.getSize}
function getMax
Gets the maximum value of the collection.
Usage example:
var collection = [1, 3, 11, 42, 4, 5, 6]; var meaningOfLife = o2.Collection.getMax(collection); // meaningOfLife will be 42 var meaningOfUniverse = o2.Collection.getMax(collection, function(item) { return item !== 42 ? Math.PI : 42; }); // meaningOfUniverse will also be 42
obj
- an Array
or an iterable
Object
to work on.
delegate
- (optional, defaults to
undefined
) the evaluator Function
in the
form functon(item, index, obj)
where item
is the current collection item; index is the index
of that item.
context
- (optional, defaults to undefined
)
the context that the delegate
uses as the this
reference. function getMethods
static
getMethods()
An alias to o2.Collection.getFunctions.
function getMin
Gets the maximum value of the collection.
Usage example:
var collection = [111, 311, 1211, 42, 114, 235, 126]; var meaningOfLife = o2.Collection.getMin(collection); // meaningOfLife will be 42 var meaningOfUniverse = o2.Collection.getMin(collection, function(item) { return item !== 42 ? 42 * Math.PI : 42; }); // meaningOfUniverse will also be 42
obj
- an Array
or an iterable
Object
to work on.
delegate
- (optional, defaults to
undefined
) the evaluator Function
in the
form functon(item, index, obj)
where item
is the current collection item; index is the index
of that item.
context
- (optional, defaults to undefined
)
the context that the delegate
uses as the this
reference. function getRest
static
getRest(Object
obj, Integer
n)
Gets the elements of the collection after index n.
Usage example:
var collection = [1, 2, 3, 4, 5]; var rest = o2.Collection.getRest(collection); // rest will be [2, 3, 4, 5] rest = o2.Collection.getRest(collection, 2); // rest will be [3, 4, 5];
obj
- an Array
or an iterable
Object
to work on.
n
- (optional; defaults to 1
) the
zero-based index to cut at. function getSize
static
getSize(Object
obj)
Gets the number of items in the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]; var size = o2.Collection.getSize(collection); // size will be 5
obj
- an Array
or an iterable
Object
to work on. function getSortedIndex
static
getSortedIndex(Array
array, Object
item, Function
delegate)
Gets an index to insert the item at a sorted Array
,
so that is not needed to be resorted.
Usage example:
var ar = [1, 2, 24, 30, 44, 66, 100]; var idx = o2.Collection.getSortedIndex(42); // idx will be 4
array
- an Array
to work on.
item
- the item to insert.
delegate
- (optional, defaults to identity function),
a Function
that takes the current item as a parameter and
returns an Integer
value. -1
if the collection is not an Array
;
the computed sorted index otherwise.
function getValues
static
getValues(Object
obj)
Gets the value of an Object
that has
{key1 : value1, key2 : value2 ... }
kind of layout.
If an Array
is passed, it makes a shallow copy of that
array and returns it.
Usage example:
var collection = {lorem : 1, ipsum : 2, dolor : 3}; var values = o2.Collection.getValues(collection); // values will be ['lorem', 'ipsum', 'dolor']
obj
- an Array
or an iterable
Object
to work on. function grep
static
grep(Object
obj, Function
delegate)
Filters the items of a collections using an evaluator delegate and returns the filtered result set.
Usage example:
var collection = [1, 2, 3, 4, 5, 6]; var evens = o2.Collection.grep(collection, function(item) { return item % 2 === 0; }); // evens will be [2, 4, 6]
obj
- an Array
or an iterable
Object
to work on.
delegate
- the filter Function
in the form
[Boolean] function(item)
. function group
static
group(Object
obj, Function
delegate)
Groups the items in the collection by a key or an evaluator
Function
.
Usage example:
var collection = [ {lorem : 1}, {lorem : 2}, {lorem : 3}, {ipsum : 1}, {ipsum : 2} ]; var lorems = o2.Collection.group(collection, 'lorem') //lorems will be {lorem : [1, 2, 3]} var grouped = o2.Collection.group(collection, function(item) { for(key in item) { if (item.hasOwnProperty(key)) { return key; } } }); // grouped will be {lorem : [1, 2, 3], ipsum : [1, 2]}
obj
- an Array
or an iterable
Object
to work on.
delegate
- A String
key that the items
in the collection share, or a Function
in the form
[key] function(item, index)
where item
is the current collection item, index if that item's
index; and the return value is a key to group. Array
of grouped items.
function inArray
static
inArray()
An alias to o2.Collection.contains
Usage example:
function includes
static
includes()
An alias to o2.Collection.contains
function indexOf
Gets the index of the element in the given Array
.
Usage example:
var ar [1, 2, 3, 4]; var id = o2.Collection.indexOf(ar, 3); // id is 2
ar
- the Array
or Object
to
search.
elm
- the Object
to match. -1
otherwise.
function intersect
static
intersect(...
varargin)
Returns an Array
of items that are common in all of
the collections passed in as parameters.
Usage example:
var ar1 = [1, 2, 3, 4, 5]; var ar2 = [2, 3, 4, 5, 6]; var ar3 = o2.Collection.intersect(ar1, ar2); // ar3 is [2, 3, 4, 5]
varargin
- the objects to intersect as input arguments. Array
containing only the values that are common
in all of the collections given.
function invoke
Calls the delegate Function
with an optional set
of parametrs for each item in the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]; function log(item) { console.log(item); } o2.Collection.invoke(collection, log); // will log: // 1 // 2 // 3 // 4 // 5
obj
- an Array
or an iterable
Object
to work on.
delegate
- the delegate to invoke in the form
delegate(item, ...varargin). If it's a String
then
item[delegate]
will be used instead.
...
- A set of parameters to pass after the delegate. function isEmpty
static
isEmpty(Object
obj)
Check whether the collection contains any members.
Usage example:
// This will return true: o2.Collection.isEmpty([]); // This will also return true: o2.Collection.isEmpty({});
obj
- an Array
or an iterable
Object
to work on. true
if the collection is empty; false
otherwise.
function lastIndexOf
Returns the last index of the given item.
Usage example:
var collection = [1, 2, 4, 2, 42, 2, 4, 42, 21, 12, 1]; var idx = o2.Collection.lastIndexOf(collection, 42); // idx will be 7
obj
- an Array
or an iterable
Object
to work on.
item
- the item to check the index of. -1
otherwise.
function map
Calls a Function
for each member of the collection,
passing the current item as a parameter. Returns an Array
containing the results of each call.
Usage example:
var collection = [1, 2, 3, 4, 5]; var squared = o2.Collection.map(collection, function(item) { return item*item; }); // squared will be [1, 4, 9, 25]
obj
- an Array
or an iterable
Object
to work on.
delegate
- A mapper in the form
function(item, index, collection)
where
item is the current collection element,
index is its index, and collection is
the current object obj.
context
- (optional, defaults to undefined
)
the context that the delegate
uses as the this
reference. Array
.
function merge
static
merge()
An alias to o2.Collection.extend.
function pluck
Hard to explain in words. Let us demonstrate by an example:
Usage example:
var collection = [ {key1 : {lorem1 : 'ipsum1'}, key2 : {dolor1 : 'amet1'}}, {key1 : {lorem2 : 'ipsum2'}, key2 : {dolor2 : 'amet2'}}, {key1 : {lorem3 : 'ipsum3'}, key2 : {dolor3 : 'amet3'}} ]; // Will return: // [ // {dolor1 : 'amet1'}, // {dolor2 : 'amet2'}, // {dolor3 : 'amet3'} // ] o2.Collection.pluck(collection, 'key2');
obj
- an Array
or an iterable
Object
to work on.
key
- the key to pluck. function reduce
Works similar to the reduce part of the Map Reduce algorithm.
Reduces a collection into a single value by
applying a delegate of the form
function(cache, value, index, collection)
where
cache is the accumulator, value
is the iterated item, index is the item's index,
and collection is the collection we are working on.
Usage example:
var collection = [1, 2, 3, 4, 5]; var reduced = o2.Collection.reduce(collection, function(store, value) { return store + value; }, 0); // reduced will be 15
obj
- an Array
or an iterable
Object
to work on.
delegate
- the reducer Function
.
store
- the initial seed.
context
- (optional, defaults to undefined
)
the context that the delegate
uses as the this
reference. function reduceRight
Works similar to o2.Collection.fold, but goes from the end of the collection to the beginning of the collection.
Usage example:
var collection = [1, 2, 3, 4, 5]; var reduced = o2.Collection.reduceRight(collection, function( store, value) { return store + value; }, 0); // reduced will be 15
obj
- an Array
or an iterable
Object
to work on.
delegate
- the reducer Functon
.
store
- the initial seed.
context
- (optional, defaults to undefined
)
the context that the delegate
uses as the this
reference. function reject
static
reject()
An alıas to o2.Collection.exclude.
function removeElement
Removes all ocurences of the element from the collection.
Usage example:
var collection = [1, 2, 3, 4, 5] var result = o2.Collection.removeElement(collection, 3); // result will be [1, 2, 4, 5]
obj
- an Array
or an iterable
Object
to work on.
elm
- the element to remove. function removeElementByValue
Removes and element from the collection if it has a property named name with a value value.
This method works by reference, and alters the given collection.
Usage example:
var collection = { lorem : 1, ipsum : 2, sit : 3} o2.Collection.removeElementByValue(collection, 'sit', 3); // collection will be {lorem : 1, ipsum : 2}
obj
- an Array
or an iterable
Object
to work on.
name
- the name of the property.
value
- the value to compare. function select
static
select()
An alias to o2.Collection.grep.
function shuffle
static
shuffle(Object
obj)
Randomizes the collection. Does not alter the original collection, just returns a randomized copy.
Usage example:
var ar = [1, 2, 3, 4, 5]; var shuffled = o2.Collection.shuffle(ar);
obj
- an Array
or an iterable
Object
to work on. Array
that's a shuffled copy of the initial
collection.
function some
Checks whether at least one element of the given collection satisfies a condition given with the delegate.
The delegate is in the form
function(context, value, index, collection)
, iterates
through the items of the collection and returns
a boolean value. When this delegate
returns true
in any iteratioin, some(...)
also returns true; it returns false
otherwise.
Usage example:
var collection = [1, 2, 3, 4, 5]; var isSome = o2.Collection.some(collection, function(item) { return item > 4; }); // isSome will be true
obj
- an Array
or an iterable
Object
to work on.
delegate
- Iterator Function
in the form
function(value, index, collection)
.
context
- The context to regard as this
reference. true
if the iterator returns
true
for at least one element; returns false
otherwise.
function sort
Sorts the collection.
Contrary to Array.prototype.sort
, this function does not
sort the collection in place, and therefore it does not alter the
initial object's contents.
Usage example:
var collection = [1, 2, 5, 4, 7]; var sorted = o2.Collection.sort(collection, function(item) { return item; }); // sorted will be [1, 2, 4, 5, 7]
obj
- an Array
or an iterable
Object
to work on.
delegate
- (Optional, defaults to an identity function
that returns the original item) the sorter in the form
function(value, index, collection)
where
value is the current item, index is
that item's index; and collection is
obj; this delegate should return an Integer
value.
function(item, index, collection)
.
context
- (optional, defaults to undefined
)
the context that the delegate
uses as the this
reference. function toArray
static
toArray(Object
obj)
Safely converts the Object
in question into anarray.
Usage example:
var obj = {lorem : 1, ipsum : 2}; var ar = o2.Collection.toArray(obj); // ar will be [1, 2]
obj
- Any Object
to convert to an
Array
. If obj is, in deed, an
Array
, then a shallow copy of it is returned without
altering the original Object
. Array
.
function union
static
union(...
varargin)
Merges several collections into a single Array
Usage example:
var union = o2.Collection.union([1, 2], [2, 3], [3, 4], [5]); // union will be [1, 2, 3, 4, 5]
varargin
- the collections to merge as input parameters. Array
.
function unique
static
unique(Object
obj, Function
delegate)
Removes duplicate entries from the collection. Returns a new
Array
; does not alter the original collection.
Usage example:
var ar = [1, 2, 3, 2, 4, 2, 42]; var uniq = o2.Collection.unique(ar); // uniq will be [1, 2, 3, 4, 42]
obj
- an Array
or an iterable
Object
to work on.
delegate
- (optional,
defaults to undefined
) a mapper in the form
function(item, index, collection)
where
item is the current collection element,
index is its index, and collection is
the current object obj. function zip
static
zip(...
varargin)
Takes a set of Array
s as parameters and brings together
the elements that have the same index.
Usage example:
var ar1 = [1,2,3]; var ar2 = ['a', 'b', 'c', 'd']; var ar3 = [true, false]; // returns: // [ // [1, 'a', true], // [2. 'b', false], // [3, 'c'], // ['d'] // ] zip(ar1, ar2, ar3);
varargin
- the Array
s to zip as a variable
number of input arguments. Array
.
A class to modify collections.