- Prototype - Periodical Execution
- Prototype - Expressing Ranges
- Prototype - AJAX Support
- Prototype - JSON Support
- Prototype - Form Management
- Prototype - Event Handling
- Prototype - Enumerating
- Prototype - Templating
- Prototype - Basic Object
- Prototype - Hash processing
- Prototype - Array Processing
- Prototype - Strings Processing
- Prototype - Number Processing
- Prototype - Element Object
- Prototype - Utility Methods
- Prototype - Useful Features
- Prototype - Short Overview
- Prototype - Home
Prototype Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Prototype - Array Processing
Prototype extends all the 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.
Using Iterators
One important support provided by Prototype is that you can use java pke 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 the above code as follows −
myArray.each(function(item) { // Your code working on item here... });
Here is the pst of all the functions with examples deapng with Array.
Prototype Array Methods
NOTE − Make sure you have the prototype.js version of 1.6.
S.No. | Method & Description |
---|---|
1. | Clears the array (makes it empty). |
2. | Returns a duppcate of the array, leaving the original array intact. |
3. | Returns a new version of the array, without any null/undefined values. |
4. | Iterates over the array in ascending numerical index order. |
5. | Returns the first item in the array, or undefined if the array is empty. |
6. | Returns a "flat" (one-dimensional) version of the array. |
7. | Clones an existing array or creates a new one from an array-pke collection. |
8. | Returns the position of the first occurrence of the argument within the array. |
9. | Returns the debug-oriented string representation of an array. |
10. | Returns the last item in the array, or undefined if the array is empty. |
11. | Reduces arrays: one-element arrays are turned into their unique element, while multiple-element arrays are returned untouched. |
12. | Returns the reversed version of the array. By default, directly reverses the original. If inpne is set to false, uses a clone of the original array. |
13. | Returns the size of the array. |
14. | This is just a local optimization of the mixed-in toArray from Enumerable. |
15. | Returns a JSON string. |
16. | Produces a duppcate-free version of an array. If no duppcates are found, the original array is returned. |
17. | Produces a new version of the array that does not contain any of the specified values. |