Skip to content
Jason Mulligan edited this page May 6, 2012 · 2 revisions

array

Array centric methods

var objAsArray = $.array.cast(obj);

array.cast

Method

Returns an Object (NodeList, etc.) as an Array

@param  {Object}  obj Object to cast
@param  {Boolean} key [Optional] Returns key or value, only applies to Objects without a length property
@return {Array}   Object as an Array

Example

var objAsArray = $.array.cast(obj);

array.clone

Method

Clones an Array

@param  {Array} obj Array to clone
@return {Array} Clone of Array

Prototype

Array.prototype.clone

Example

var arClone = myArray.clone();

array.contains

Method

Finds the index of arg(s) in instance

@param  {Array}  obj  Array to search
@param  {String} arg  Comma delimited string of search values
@return {Mixed}  Integer or an array of integers representing the location of the arg(s)

Prototype

Array.prototype.contains

Example

if (myArray.contains("someValue, anotherValue").length > 0) doSomething();

array.diff

Method

Finds the difference between array1 and array2

@param  {Array} array1 Source Array
@param  {Array} array2 Comparison Array
@return {Array} Array of the differences

Prototype

Array.prototype.diff

Example

var result = ["abc", "xyz"].diff(["abc"]); // result = ["xyz"]

array.each

Method

Iterates obj and executes fn, parameters for fn are 'value', 'key'

@param  {Array}    obj Array to iterate
@param  {Function} fn  Function to execute on index values
@return {Array} Array

Prototype

Array.prototype.each

Example

myArray.each(function (i) { doSomething(i); });

array.first

Method

Returns the first Array node

@param  {Array} obj The array
@return {Mixed} The first node of the array

Prototype

Array.prototype.first

Example

var x = myArray.first();

array.index

Method

Facade to indexOf for shorter syntax

@param  {Array} obj Array to search
@param  {Mixed} arg Value to find index of
@return {Number} The position of arg in instance

Prototype

Array.prototype.index

Example

if (myArray.index("someValue") > -1) doSomething();

array.indexed

Method

Returns an Associative Array (Object) as an Indexed Array of values

@param  {Array} obj Array to index
@return {Array} Indexed Array

Prototype

Array.prototype.indexed

Example

var result = $.array.indexed({abc:true, xyz:{hhh:[true, false]}}) // result = [true, true, false]

array.intersect

Method

Finds the intersections between array1 and array2

@param  {Array} array1 Source Array
@param  {Array} array2 Comparison Array
@return {Array} Array of the intersections

Prototype

Array.prototype.intersect

Example

var result = ["abc", "xyz"].intersect(["abc"]); // result = ["abc"]

array.keys

Method

Returns the keys in an Associative Array (Object)

@param  {Array} obj Array to extract keys from
@return {Array} Array of the keys

Prototype

Array.prototype.keys

Example

var result = $.array.keys({abc:true}); // result = ["abc"]

array.last

Method

Returns the last node of the array

@param  {Array} obj Array
@return {Mixed} Last node of Array

Prototype

Array.prototype.last

Example

var x = myArray.last();

array.range

Method

Returns a range of indices from the Array

@param  {Number} start Starting index
@param  {Number} end   Ending index
@returns {Array} Array of indices

Prototype

Array.prototype.range

Example

var result = ar.range(0, 4);

array.remove

Method

Removes indices from an Array without recreating it

@param  {Array}   obj   Array to remove from
@param  {Number} start Starting index
@param  {Number} end   [Optional] Ending index
@return {Array} Modified Array

Prototype

Array.prototype.remove

Example

["abc", "xyz"].remove("xyz"); // ["abc"]

array.toObject

Method

Casts an Array to Object

@param  {Array} ar Array to transform
@return {Object} New object

Prototype

Array.prototype.toObject

Example

var result = ["abc"].toObject(); // result = {0: "abc"}

array.total

Method

Gets the total keys in an Associative Array

@param  {Array} obj Array to find the length of
@return {Number} Number of keys in Array

Prototype

Array.prototype.total

Example

var myArray = [];
myArray.abc = true;
myArray.total(); // 1