Object
Array
The Array
extension builds upon the existing JavaScript Array
object, providing new methods as well as standardized ones. This extension ensures
that the Array
object has access to the latest methods as Defined by ECMA-262, 3rd edition, as well as the new Mozilla-based extensions.
When loaded, this extension looks to see if the methods Defined by ECMA-262 and Mozilla 1.8 already exist. If so, these methods are not overridden. For thoes browsers that don't support these methods natively, they will be added so developers can seemlessly use these methods as if natively implemented. Additionally, several non-standard methods are added to everyone's implementation for convenience.
Nicholas C. Zakas, http://www.nczonline.net/
void |
append(variant item[]) Appends any number of items to the end of the array. |
Array |
clone() Creates and returns a duplicate of the array. |
boolean |
contains(variant item) Determines if a given item is in the array. |
boolean |
every(Function test) Runs a test on each item in the array and returns the result: true
if all items pass, false if not. |
boolean |
every(Function
test, Object base) Runs a test on each item in the array and returns the result: true
if all items pass, false if not. |
Array |
filter(Function test) Runs a test on each item in the array and returns an array with all items that passed the test. |
Array |
filter(Function
test, Object base) Runs a test on each item in the array and returns an array with all items that passed the test. |
void |
forEach(function
test) Runs a function on each item in the array. |
void |
forEach(function
test, Object base) Runs a function on each item in the array. |
int |
indexOf(variant item) Returns the position of a given item in the array starting from the first item, -1 if not found. |
int |
indexOf(variant
item, int start) Returns the position of a given item in the array starting from the given position, -1 if not found. |
void |
insertAt(variant
item, int pos) Inserts an item into the given position. |
void |
insertBefore(variant
item, variant beforeItem) Inserts an item before the given item. |
int |
lastIndexOf(variant
item) Return the position of a given item in the array starting from the last item, -1 if not found. |
int |
lastIndexOf(variant
item, int start) Return the position of a given item in the array starting from the given start position, -1 if not found. |
Array |
map(Function exec) Runs a function on each item in the array and returns an array with the results. |
Array |
map(Function
exec, Object base) Runs a function on each item in the array and returns an array with the results. |
variant |
pop() Removes the last item in the array and return sit. |
void |
push(variant item[]) Appends any number of items to the end of the array. |
variant |
remove(variant item) Removes the given item from the array and returns it. |
variant |
removeAt(int pos) Removes the item in the given position from the array and returns it. |
variant |
shift() Removes the first item in the array and returns it. |
Array |
slice(int start, int
stop) Creates and returns a new array made up of the items between the given positions. |
boolean |
some(Function test) Runs a test on each item in the array and returns the result: true
if at least one item passes the test, false if not. |
boolean |
some(Function
test, Object base) Runs a test on each item in the array and returns the result: true
if at least one item passes the test, false if not. |
Array |
splice(int pos, int
length) Removes the item starting at the given position and ending with that position plus the length specified. |
Array |
splice(int
pos, int length, variant item[]) Removes the item starting at the given position and ending with that position plus the length specified. |
variant |
sum() Returns the sum of the items in the array. |
variant |
sum(Function convert) Runs a conversion function on each item in the array and returns the sum of these values. |
variant |
sum(Function
convert, Object base) Runs a conversion function on each item in the array and returns the sum of these values. |
void |
unshift(variant
item[]) Prepends any number of items to the front of the array. |
public void append(variant item[])
Appends any number of items to the end of the array.
item[]
- the items to add.public Array clone()
Creates and returns a duplicate of the array.
An exact copy of the array.
Nicholas C. Zakas, http://www.nczonline.net
public boolean contains(variant item)
Determines if a given item is in the array.
item
- the item to find.A boolean value: true
if the item is in the array, false
if not.
Nicholas C. Zakas, http://www.nczonline.net
public boolean every(Function test)
Runs a test on each item in the array and returns true
if all
items pass the test.
test
- the function to run on each item.A boolean value: true
if all items pass the test, false
if not.
public boolean every(Function test, Object base)
Runs a test on each item in the array and returns true
if all
items pass the test.
test
- the object method to run on each item.base
- the object that the method belongs to.A boolean value: true
if all items pass the test, false
if not.
Mozilla 1.8
public Array filter(Function test, Object base)
Runs a test on each item in the array and returns an array of all items that passed the test.
test
- the object method to run on each item.base
- the object that the method belongs to.An array of items that passed the test.
public void forEach(Function exec)
Runs a function on each item in the array.
exec
- the function to run on each item.public void foreach(Function exec, Object base)
Runs a function on each item in the array.
exec
- the object method to run on each item.base
- the object that the method belongs to.public int indexOf(variant item)
Returns the position of the given item starting at the front of the array, -1 if not found.
item
- the item to find in the array.The position of the item in the array or -1 if not found.
public int indexOf(variant item, int start)
Returns the position of the given item starting at the given position from the front of the array, -1 if not found.
item
- the item to find in the array.start
- the first position that should be checked.The position of the item in the array or -1 if not found.
public void insertAt(variant item, int pos)
Inserts an item into the array at a given position.
item
- the item to insert into the array.pos
- the position in which the item should be placed.Nicholas C. Zakas, http://www.nczonline.net/
public void insertBefore(variant item, variant beforeItem)
Inserts an item into the array before the given item.
item
- the item to insert into the array.beforeItem
- the item before which the inserted item should
be placed.Nicholas C. Zakas, http://www.nczonline.net/
public int lastIndexOf(variant item)
Returns the position of the given item starting at the back of the array, -1 if not found.
item
- the item to find in the array.The position of the item in the array or -1 if not found.
public int lastIndexOf(variant item, int start)
Returns the position of the given item starting at the given position from the back of the array, -1 if not found.
item
- the item to find in the array.start
- the first position that should be checked.The position of the item in the array or -1 if not found.
public Array map(Function exec)
Runs a function on each item in the array and returns an array of the results.
exec
- the function to run on each item.An array of results based on the running of the function on each item.
public Array map(Function exec, Object base)
Runs a function on each item in the array and returns an array of the results.
exec
- the object method to run on each item.base
- the object that the method belongs to.An array of results based on the running of the function on each item.
public variant pop()
Removes the last item in the array and returns it.
The item that was removed.
public void push(variant item[])
Appends any number of items to the end of the array.
item[]
- the items to add.public variant remove(variant item)
Removes the given item from the array and returns it.
item
- the item to remove.The item that was removed.
Nicholas C. Zakas, http://www.nczonline.net/
public variant removeAt(int pos)
Removes the item at the given position and returns it.
pos
- the position of the item to remove.The item that was removed.
Nicholas C. Zakas, http://www.nczonline.net/
public variant shift()
Removes the first item in the array and returns it.
The item that was removed.
ECMA-262, 3rd Edition
public Array slice(int start, int stop)
Creates and returns a new array made up of the items between the given positions.
start
- the first item to add to the array.stop
- the item after the last item to add to the array.An array with the corresponding values.
ECMA-262, 3rd Edition
public boolean some(Function test)
Runs a test on each item in the array and returns the result: true
if at least one item passes the test, false
if not.
test
- the function to run on each item.A boolean value: true
if at least one item passes the test, false
if not.
Mozilla 1.8
public boolean some(Function test, Object base)
Runs a test on each item in the array and returns the result: true
if at least one item passes the test, false
if not.
test
- the object method to run on each item.base
- the object that owns the method.A boolean value: true
if at least one item passes the test, false
if not.
Mozilla 1.8
public Array splice(int pos, int length)
Removes the item starting at the given position and ending with that position plus the length specified.
pos
- the position of the first item to remove.length
- the number of items to remove, 0 to not remove.An array containing all of the items that were removed during the operation.
ECMA-262, 3rd Edition
public Array splice(int pos, int length, variant item[])
Removes the item starting at the given position and ending with that position plus the length specified.
pos
- the position of the first item to remove.length
- the number of items to remove, 0 to not remove.item[]
- the items to add to the array in place of the removed
items. An array containing all of the items that were removed during the operation.
ECMA-262, 3rd Edition
public variant sum()
Returns the sum of the values in the array.
Either a string (if the array contains at least one string or object) or a number (if all items are numbers).
Nicholas C. Zakas, http://www.nczonline.net/
public variant sum(Function convert)
Runs a conversion function on each item in the array and returns the sum of these values.
convert
- the conversion function to run on each item.Either a string (if the array contains at least one string or object) or a number (if all items are numbers).
Nicholas C. Zakas, http://www.nczonline.net
public variant sum(Function convert, Object base)
Runs a conversion function on each item in the array and returns the sum of these values.
test
- the conversion method to run on each item.base
- the object that the method belongs to.Either a string (if the array contains at least one string or object) or a number (if all items are numbers).
Nicholas C. Zakas, http://www.nczonline.net/
public void unshift(variant item[])
Prepends any number of items to the front of the array. This is the opposite
of shift()
.
item[]
- the items to add to the array.ECMA-262, 3rd Edition