Packageweavejs.util
Classpublic class ArrayUtils
InheritanceArrayUtils Inheritance Object

This class contains static functions that manipulate Arrays.



Public Methods
 MethodDefined By
  
binarySearch(sortedUniqueValues:Array, item:*, exactMatchOnly:Boolean, compare:Function = null):int
[static] Performs a binary search on a sorted array with no duplicate values.
ArrayUtils
  
compareProperties(object1:Object, object2:Object, propertyNames:Array):int
[static] Compares a list of properties in two objects
ArrayUtils
  
copy(source:Array, destination:Array = null):Array
[static] This function copies the contents of the source to the destination.
ArrayUtils
  
createLookup(array:*, ... propertyChain):Object
[static] Creates a lookup from item (or item property) to index.
ArrayUtils
  
fillKeys(output:Object, keys:Array):void
[static] Fills an Object with the keys from an Array.
ArrayUtils
  
flatten(source:Array, destination:Array = null):Array
[static] This will flatten an Array of Arrays into a flat Array.
ArrayUtils
  
flattenObject(input:Object, output:Object = null, prefix:String):Object
[static]
ArrayUtils
  
getItems(object:*, keys:Array, output:* = null):*
[static] This will get a subset of properties/items/attributes from an Object/Array/XML.
ArrayUtils
  
getMedianIndex(list:Array, compareFunction:Function, firstIndex:int = 0, lastIndex:int = -1):int
[static] See http://en.wikipedia.org/wiki/Quick_select#Partition-based_general_selection_algorithm
ArrayUtils
  
intersection(firstArray:Array, secondArray:Array, ... moreArrays):Array
[static] Computes the intersection of the items in a list of two or more Arrays.
ArrayUtils
  
isEmpty(object:Object):Boolean
[static] If there are any properties of the Object, return false; else, return true.
ArrayUtils
  
joinItems(arrayOfArrays:Array, separator:String, includeEmptyItems:Boolean):Array
[static] This will take an Array of Arrays of String items and produce a single list of String-joined items.
ArrayUtils
  
mergeSorted(sortedInputA:Array, sortedInputB:Array, mergedOutput:Array, comparator:Function):void
[static] Merges two previously-sorted arrays.
ArrayUtils
  
pluck(array:Array, property:String):Array
[static] Gets a list of values of a property from a list of objects.
ArrayUtils
  
randomSort(array:Array):void
[static] randomizes the order of the elements in the Array in O(n) time by modifying the given array.
ArrayUtils
  
removeByIndex(array:Array, indices:Array):void
[static] Removes items from an Array.
ArrayUtils
  
[static] Efficiently removes duplicate adjacent items in a pre-sorted Array.
ArrayUtils
  
subtract(array:Array, itemsToRemove:Array):Array
[static] Removes items from an Array.
ArrayUtils
  
transpose(table:Array):Array
[static] Transposes a two-dimensional table.
ArrayUtils
  
union(... arrays):Array
[static] Computes the union of the items in a list of Arrays.
ArrayUtils
  
zipObject(keys:Array, values:Array):Object
[static] Creates an object from arrays of keys and values.
ArrayUtils
Method Detail
binarySearch()method
public static function binarySearch(sortedUniqueValues:Array, item:*, exactMatchOnly:Boolean, compare:Function = null):int

Performs a binary search on a sorted array with no duplicate values.

Parameters

sortedUniqueValues:Array — Array of Numbers or Strings
 
item:* — A compare function
 
exactMatchOnly:Boolean — If true, searches for exact match. If false, searches for insertion point.
 
compare:Function (default = null)

Returns
int — The index of the matching value or insertion point.
compareProperties()method 
public static function compareProperties(object1:Object, object2:Object, propertyNames:Array):int

Compares a list of properties in two objects

Parameters

object1:Object — The first object
 
object2:Object — The second object
 
propertyNames:Array — A list of names of properties to compare

Returns
int — -1, 0, or 1
copy()method 
public static function copy(source:Array, destination:Array = null):Array

This function copies the contents of the source to the destination. Either parameter may be either an Array.

Parameters

source:Array — An Array-like object.
 
destination:Array (default = null) — An Array.

Returns
Array — A pointer to the destination Array
createLookup()method 
public static function createLookup(array:*, ... propertyChain):Object

Creates a lookup from item (or item property) to index. Does not consider duplicate items (or duplicate item property values).

Parameters

array:* — An Array or Object
 
... propertyChain — A property name or chain of property names to index on rather than the item itself.

Returns
Object — A reverse lookup Map.
fillKeys()method 
public static function fillKeys(output:Object, keys:Array):void

Fills an Object with the keys from an Array.

Parameters

output:Object
 
keys:Array

flatten()method 
public static function flatten(source:Array, destination:Array = null):Array

This will flatten an Array of Arrays into a flat Array. Items will be appended to the destination Array.

Parameters

source:Array — A multi-dimensional Array to flatten.
 
destination:Array (default = null) — An Array to append items to. If none specified, a new one will be created.

Returns
Array — The destination Array with all the nested items in the source appended to it.
flattenObject()method 
public static function flattenObject(input:Object, output:Object = null, prefix:String):Object

Parameters

input:Object
 
output:Object (default = null)
 
prefix:String

Returns
Object
getItems()method 
public static function getItems(object:*, keys:Array, output:* = null):*

This will get a subset of properties/items/attributes from an Object/Array/XML.

Parameters

object:* — An Object/Array containing properties/items to retrieve.
 
keys:Array — A list of property names, index values.
 
output:* (default = null) — Optionally specifies where to store the resulting items.

Returns
* — An Object (or Array) containing the properties/items/attributes specified by keysOrIndices.
getMedianIndex()method 
public static function getMedianIndex(list:Array, compareFunction:Function, firstIndex:int = 0, lastIndex:int = -1):int

See http://en.wikipedia.org/wiki/Quick_select#Partition-based_general_selection_algorithm

Parameters

list:Array — An Array to be re-organized.
 
compareFunction:Function — A function that takes two array elements a,b and returns -1 if a<b, 1 if a>b, or 0 if a==b.
 
firstIndex:int (default = 0) — The index of the first element in the list to calculate a median from.
 
lastIndex:int (default = -1) — The index of the last element in the list to calculate a median from.

Returns
int — The index the median element.
intersection()method 
public static function intersection(firstArray:Array, secondArray:Array, ... moreArrays):Array

Computes the intersection of the items in a list of two or more Arrays.

Parameters

firstArray:Array
 
secondArray:Array
 
... moreArrays

Returns
Array — The intersection of the items appearing in all Arrays, in the order that they appear in the first Array.
isEmpty()method 
public static function isEmpty(object:Object):Boolean

If there are any properties of the Object, return false; else, return true.

Parameters

object:Object — The Object to test for emptiness.

Returns
Boolean — A boolean which is true if the Object is empty, false if it has at least one property.
joinItems()method 
public static function joinItems(arrayOfArrays:Array, separator:String, includeEmptyItems:Boolean):Array

This will take an Array of Arrays of String items and produce a single list of String-joined items.

Parameters

arrayOfArrays:Array — An Array of Arrays of String items.
 
separator:String — The separator String used between joined items.
 
includeEmptyItems:Boolean — Set this to true to include empty-strings and undefined items in the nested Arrays.

Returns
Array — An Array of String-joined items in the same order they appear in the nested Arrays.
mergeSorted()method 
public static function mergeSorted(sortedInputA:Array, sortedInputB:Array, mergedOutput:Array, comparator:Function):void

Merges two previously-sorted arrays.

Parameters

sortedInputA:Array — The first sorted array.
 
sortedInputB:Array — The second sorted array.
 
mergedOutput:Array — An array to store the merged arrays.
 
comparator:Function — A function that takes two parameters and returns -1 if the first parameter is less than the second, 0 if equal, or 1 if the first is greater than the second.

pluck()method 
public static function pluck(array:Array, property:String):Array

Gets a list of values of a property from a list of objects.

Parameters

array:Array — An Array of Objects.
 
property:String — The property name to get from each object

Returns
Array — A list of the values of the specified property for each object in the original list.
randomSort()method 
public static function randomSort(array:Array):void

randomizes the order of the elements in the Array in O(n) time by modifying the given array.

Parameters

array:Array — the array to randomize

removeByIndex()method 
public static function removeByIndex(array:Array, indices:Array):void

Removes items from an Array.

Parameters

array:Array — Array
 
indices:Array — Array of numerically sorted indices to remove

removeDuplicatesFromSortedArray()method 
public static function removeDuplicatesFromSortedArray(sorted:Array):void

Efficiently removes duplicate adjacent items in a pre-sorted Array.

Parameters

sorted:Array — The sorted Array

subtract()method 
public static function subtract(array:Array, itemsToRemove:Array):Array

Removes items from an Array.

Parameters

array:Array — An Array of items.
 
itemsToRemove:Array — An Array of items to skip when making a copy of the array.

Returns
Array — A new Array containing the items from the original array except those that appear in itemsToRemove.
transpose()method 
public static function transpose(table:Array):Array

Transposes a two-dimensional table.

Parameters

table:Array

Returns
Array
union()method 
public static function union(... arrays):Array

Computes the union of the items in a list of Arrays. Can also be used to get a list of unique items in an Array.

Parameters

... arrays — A list of Arrays.

Returns
Array — The union of all the unique items in the Arrays in the order they appear.
zipObject()method 
public static function zipObject(keys:Array, values:Array):Object

Creates an object from arrays of keys and values.

Parameters

keys:Array — Keys corresponding to the values.
 
values:Array — Values corresponding to the keys.

Returns
Object — A new Object.