Copyright © tutorialspoint.com
Prototype extends all native JavaScript arrays with quite a few powerful methods.
This is done in two ways:
It mixes in the Enumerable module, which brings a ton of methods in already.
It adds quite a few extra methods, which are documented in this section.
One important support provided by Prototype is that you can use java like iterator in JavaScript. See the difference below:
Traditional way of writing a for loop:
for (var index = 0; index < myArray.length; ++index) { var item = myArray[index]; // Your code working on item here... } |
Now if you are using Prototype then you can replace above code as follows:
myArray.each(function(item) { // Your code working on item here... }); |
Here is the list of all the functions with examples dealing with Array.
NOTE: Make sure you have at least version 1.6 of prototype.js.
Methods | Description |
---|---|
clear() | Clears the array (makes it empty). |
clone() | Returns a duplicate of the array, leaving the original array intact. |
compact() | Returns a new version of the array, without any null/undefined values. |
each() | Iterates over the array in ascending numerical index order. |
first() | Returns the first item in the array, or undefined if the array is empty. |
flatten() | Returns a "flat" (one-dimensional) version of the array. |
from() | Clones an existing array or creates a new one from an array-like collection. |
indexOf() | Returns the position of the first occurrence of the argument within the array. |
inspect() | Returns the debug-oriented string representation of an array. |
last() | Returns the last item in the array, or undefined if the array is empty. |
reduce() | Reduces arrays: one-element arrays are turned into their unique element, while multiple-element arrays are returned untouched. |
reverse() | Returns the reversed version of the array. By default, directly reverses the original. If inline is set to false, uses a clone of the original array. |
size() | Returns the size of the array. |
toArray() | This is just a local optimization of the mixed-in toArray from Enumerable. |
toJSON() | Returns a JSON string. |
uniq() | Produces a duplicate-free version of an array. If no duplicates are found, the original array is returned. |
without() | Produces a new version of the array that does not contain any of the specified values. |
Copyright © tutorialspoint.com