* Removing the parseXML functionality. Will be moved into More or into a separate...
[mootools/dkf.git] / Docs / Types / Array.md
blob1f90480dad4b4425decd15aff2cf02cb139a3b42
1 Type: Array {#Array}
2 ====================
4 A collection of Array methods and functions.
6 ### See Also:
8 - [MDC Array][]
11 Function: Array.each {#Array:Array-each}
12 ----------------------------------
14 Used to iterate through arrays, or iterables that are not regular arrays, such as built in getElementsByTagName calls or arguments of a function.
16 ### Syntax:
18         Array.each(iterable, fn[, bind]);
20 ### Arguments:
22 1. iterable - (*array*) The array to iterate through.
23 2. fn       - (*function*) The function to test for each element.
24 3. bind     - (*object*, optional) The object to use as 'this' within the function. For more information see [Function:bind][].
26 #### Argument: fn
28 ##### Syntax:
30         fn(item, index, object)
32 ##### Arguments:
34 1. item   - (*mixed*) The current item in the array.
35 2. index  - (*number*) The current item's index in the array. In the case of an object, it is passed the key of that item rather than the index.
36 3. object - (*mixed*) The actual array/object.
38 ### Example:
40         Array.each(['Sun', 'Mon', 'Tue'], function(day, index){
41                 alert('name:' + day + ', index: ' + index);
42         }); // alerts 'name: Sun, index: 0', 'name: Mon, index: 1', etc.
44 ### See Also:
46 - [Array:each](#Array:each)
48 ### Notes:
50 This is an array-specific equivalent of *$each* from MooTools 1.2.
54 Function: Array.clone {#Array:Array-clone}
55 ------------------------------------
57 Returns a copy of the passed array.
59 ### Syntax:
61         var clone = Array.clone(myArray);
63 ### Arguments:
65 1. myArray      - (*array*) The array you wish to copy.
67 ### Returns:
69 * (*array*) a copy of the passed array.
71 ### Example:
73         var myArray = ['red', 'blue', 'green'];
74         var otherArray = Array.clone(myArray);
76         var myArray[0] = 'yellow';
78         alert(myArray[0]);              // alerts 'yellow'
79         alert(otherArray[0])    // alerts 'red'
81 ### Notes:
83 This is an array-specific equivalent of *$unlink* from MooTools 1.2.
87 Function: Array.from {#Array:Array-from}
88 ----------------------------------
90 Converts the argument passed in to an array if it is defined and not already an array.
92 ### Syntax:
94         var splatted = Array.from(obj);
96 ### Arguments:
98 1. obj - (*mixed*) Any type of variable.
100 ### Returns:
102 * (*array*) If the variable passed in is an array, returns the array. Otherwise, returns an array with the only element being the variable passed in.
104 ### Example:
106         Array.from('hello'); // returns ['hello'].
107         Array.from(['a', 'b', 'c']); // returns ['a', 'b', 'c'].
109 ### Notes:
111 This is equivalent to *$splat* from MooTools 1.2.
115 Array method: each {#Array:each}
116 ---------------------------------
118 Calls a function for each element in the array.
120 ### Syntax:
122         myArray.each(fn[, bind]);
124 ### Arguments:
126 1. fn   - (*function*) The function which should be executed on each item in the array. This function is passed the item and its index in the array.
127 2. bind - (*object*, optional) The object to be used as 'this' in the function. For more information see [Function:bind][].
129 #### Argument: fn
131 ##### Syntax
133         fn(item, index, array)
135 ##### Arguments:
137 1. item   - (*mixed*) The current item in the array.
138 2. index  - (*number*) The current item's index in the array.
139 3. array  - (*array*) The actual array.
141 ### Examples:
143         //Alerts "0 = apple", "1 = banana", and so on:
144         ['apple', 'banana', 'lemon'].each(function(item, index){
145                 alert(index + " = " + item);
146         }); //The optional second argument for binding isn't used here.
149 ### See Also:
151 - [Array.each](#Array:Array-each)
152 - [MDC Array:forEach][]
154 ### Notes:
156 - This method is only available for browsers without native [MDC Array:forEach][] support.
163 Array method: invoke {#Array:invoke}
164 --------------------------
166 Returns an array with the named method applied to the array's contents.
168 ### Syntax:
170         var arr = myArray.invoke(method[, arg, arg, arg ...])
172 ### Arguments:
174 1. method - (*string*) The method to apply to each item in the array.
175 2. arg - (*mixed*) Any number of arguments to pass to the named method.
177 ### Returns:
179 * (*array*) A new array containing the results of the applied method.
181 ### Example:
183         var foo = [4, 8, 15, 16, 23, 42];
184         var bar = foo.invoke('limit', 10, 30);  //bar is now [10, 10, 15, 16, 23, 30]
188 Array method: every {#Array:every}
189 ----------------------------
191 Returns true if every element in the array satisfies the provided testing function.
192 This method is provided only for browsers without native [Array:every][] support.
194 ### Syntax:
196         var allPassed = myArray.every(fn[, bind]);
198 ### Arguments:
200 1. fn   - (*function*) The function to test for each element.
201 2. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind][].
203 #### Argument: fn
205 ##### Syntax:
207         fn(item, index, array)
209 ##### Arguments:
211 1. item   - (*mixed*) The current item in the array.
212 2. index  - (*number*) The current item's index in the array.
213 3. array  - (*array*) The actual array.
215 ### Returns:
217 * (*boolean*) If every element in the array satisfies the provided testing function, returns true. Otherwise, returns false.
219 ### Examples:
221         var areAllBigEnough = [10, 4, 25, 100].every(function(item, index){
222                 return item > 20;
223         }); // areAllBigEnough = false
226 ### See Also:
228 - [MDC Array:every][]
232 Array method: filter {#Array:filter}
233 ------------------------------
235 Creates a new array with all of the elements of the array for which the provided filtering function returns true.
236 This method is provided only for browsers without native [Array:filter][] support.
238 ### Syntax:
240         var filteredArray = myArray.filter(fn[, bind]);
242 ### Arguments:
244 1. fn   - (*function*) The function to test each element of the array. This function is passed the item and its index in the array.
245 2. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind][].
247 #### Argument: fn
249 ##### Syntax:
251         fn(item, index, array)
253 ##### Arguments:
255 1. item   - (*mixed*) The current item in the array.
256 2. index  - (*number*) The current item's index in the array.
257 3. array  - (*array*) The actual array.
259 ### Returns:
261 * (*array*) The new filtered array.
263 ### Examples:
265         var biggerThanTwenty = [10, 3, 25, 100].filter(function(item, index){
266                 return item > 20;
267         }); // biggerThanTwenty = [25, 100]
269 ### See Also:
271 - [MDC Array:filter][]
275 Array method: clean {#Array:clean}
276 ----------------------------
278 Creates a new array with all of the elements of the array which are defined (i.e. not null or undefined).
280 ### Syntax:
282         var cleanedArray = myArray.clean();
284 ### Returns:
286 * (*array*) The new filtered array.
288 ### Examples:
290         var myArray = [null, 1, 0, true, false, 'foo', undefined, ''];
291         myArray.clean() // returns [1, 0, true, false, 'foo', '']
295 Array method: indexOf {#Array:indexOf}
296 --------------------------------
298 Returns the index of the first element within the array equal to the specified value, or -1 if the value is not found.
299 This method is provided only for browsers without native [Array:indexOf][] support.
301 ### Syntax:
303         var index = myArray.indexOf(item[, from]);
305 ### Returns:
307 * (*number*) The index of the first element within the array equal to the specified value. If not found, returns -1.
309 ### Arguments:
311 1. item - (*object*) The item to search for in the array.
312 2. from - (*number*, optional: defaults to 0) The index of the array at which to begin the search.
314 ### Examples:
316         ['apple', 'lemon', 'banana'].indexOf('lemon'); // returns 1
317         ['apple', 'lemon'].indexOf('banana'); // returns -1
319 ### See Also:
321 - [MDC Array:indexOf][]
325 Array method: map {#Array:map}
326 ------------------------
328 Creates a new array with the results of calling a provided function on every element in the array.
329 This method is provided only for browsers without native [Array:map][] support.
331 ### Syntax:
333         var mappedArray = myArray.map(fn[, bind]);
335 ### Arguments:
337 1. fn   - (*function*) The function to produce an element of the new Array from an element of the current one.
338 2. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind][].
340 #### Argument: fn
342 ##### Syntax:
344         fn(item, index, array)
346 ##### Arguments:
348 1. item   - (*mixed*) The current item in the array.
349 2. index  - (*number*) The current item's index in the array.
350 3. array  - (*array*) The actual array.
352 ### Returns:
354 * (*array*) The new mapped array.
356 ### Examples:
358         var timesTwo = [1, 2, 3].map(function(item, index){
359                 return item * 2;
360         }); //timesTwo = [2, 4, 6];
362 ### See Also:
364 - [MDC Array:map][]
368 Array method: some {#Array:some}
369 --------------------------
371 Returns true if at least one element in the array satisfies the provided testing function.
372 This method is provided only for browsers without native [Array:some][] support.
374 ### Syntax:
376         var somePassed = myArray.some(fn[, bind]);
378 ### Returns:
380 * (*boolean*) If at least one element in the array satisfies the provided testing function returns true. Otherwise, returns false.
382 ### Arguments:
384 1. fn   - (*function*) The function to test for each element. This function is passed the item and its index in the array.
385 2. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind][].
387 #### Argument: fn
389 ##### Syntax:
391         fn(item, index, array)
393 ##### Arguments:
395 1. item   - (*mixed*) The current item in the array.
396 2. index  - (*number*) The current item's index in the array.
397 3. array  - (*array*) The actual array.
399 ### Examples:
401         var isAnyBigEnough = [10, 4, 25, 100].some(function(item, index){
402                 return item > 20;
403         }); // isAnyBigEnough = true
405 ### See Also:
407 - [MDC Array:some][]
411 Array method: associate {#Array:associate}
412 ------------------------------------
414 Creates an object with key-value pairs based on the array of keywords passed in and the current content of the array.
416 ### Syntax:
418         var associated = myArray.associate(obj);
420 ### Arguments:
422 1. obj - (*array*) Its items will be used as the keys of the object that will be created.
424 ### Returns:
426 * (*object*) The new associated object.
428 ### Examples:
430         var animals = ['Cow', 'Pig', 'Dog', 'Cat'];
431         var sounds = ['Moo', 'Oink', 'Woof', 'Miao'];
432         sounds.associate(animals);
433         // returns {'Cow': 'Moo', 'Pig': 'Oink', 'Dog': 'Woof', 'Cat': 'Miao'}
437 Array method: link {#Array:link}
438 --------------------------
440 Accepts an object of key / function pairs to assign values.
442 ### Syntax:
444         var result = myArray.link(object);
446 ### Arguments:
448 1. object - (*object*)  An object containing key / function pairs must be passed to be used as a template for associating values with the different keys.
450 ### Returns:
452 * (*object*) The new associated object.
454 ### Examples:
456         var el = document.createElement('div');
457         var arr2 = [100, 'Hello', {foo: 'bar'}, el, false];
458         arr2.link({
459                 myNumber: Type.isNumber,
460                 myElement: Type.isElement,
461                 myObject: Type.isObject,
462                 myString: Type.isString,
463                 myBoolean: function(obj){ return obj != null; }
464         });
465         // returns {myNumber: 100, myElement: el, myObject: {foo: 'bar'}, myString: 'Hello', myBoolean: false}
468 Array method: contains {#Array:contains}
469 ----------------------------------
471 Tests an array for the presence of an item.
473 ### Syntax:
475         var inArray = myArray.contains(item[, from]);
477 ### Arguments:
479 1. item - (*object*) The item to search for in the array.
480 2. from - (*number*, optional: defaults to 0) The index of the array at which to begin the search.
482 ### Returns:
484 * (*boolean*) If the array contains the item specified, returns true. Otherwise, returns false.
486 ### Examples:
488         ['a', 'b', 'c'].contains('a'); // returns true
489         ['a', 'b', 'c'].contains('d'); // returns false
491 ### See Also:
493 - [MDC Array:indexOf][]
497 Array method: append {#Array:append}
498 ------------------------------
500 Appends the passed array to the end of the current array.
502 ### Syntax:
504         var myArray = myArray.append(otherArray);
506 ### Arguments:
508 1. otherArray - (*array*) The array containing values you wish to append.
510 ### Returns:
512 * (*array*) The original array including the new values.
514 ### Examples:
516         var myOtherArray = ['green', 'yellow'];
517         ['red', 'blue'].append(myOtherArray); // returns ['red', 'blue', 'green', 'yellow'];
519 ### Notes:
521 This is an array-specific equivalent of *$extend* from MooTools 1.2.
525 Array method: getLast {#Array:getLast}
526 --------------------------------
528 Returns the last item from the array.
530 ### Syntax:
532         myArray.getLast();
534 ### Returns:
536 * (*mixed*) The last item in this array.
537 * (*null*) If this array is empty, returns null.
539 ### Examples:
541         ['Cow', 'Pig', 'Dog', 'Cat'].getLast(); // returns 'Cat'
545 Array method: getRandom {#Array:getRandom}
546 ------------------------------------
548 Returns a random item from the array.
550 ### Syntax:
552         myArray.getRandom();
554 ### Returns:
556 * (*mixed*) A random item from this array. If this array is empty, returns null.
558 ### Examples:
560         ['Cow', 'Pig', 'Dog', 'Cat'].getRandom(); // returns one of the items
564 Array method: include {#Array:include}
565 --------------------------------
567 Pushes the passed element into the array if it's not already present (case and type sensitive).
569 ### Syntax:
571         myArray.include(item);
573 ### Arguments:
575 1. item - (*object*) The item that should be added to this array.
577 ### Returns:
579 * (*array*) This array with the new item included.
581 ### Examples:
583         ['Cow', 'Pig', 'Dog'].include('Cat'); // returns ['Cow', 'Pig', 'Dog', 'Cat']
584         ['Cow', 'Pig', 'Dog'].include('Dog'); // returns ['Cow', 'Pig', 'Dog']
586 ### Notes:
588 If you want to push the passed element even if it's already present, use
589 the vanilla javascript:
591         myArray.push(item);
593 Array method: combine {#Array:combine}
594 --------------------------------
596 Combines an array with all the items of another. Does not allow duplicates and is case and type sensitive.
598 ### Syntax:
600         myArray.combine(array);
602 ### Arguments:
604 1. array - (*array*) The array whose items should be combined into this array.
606 ### Returns:
608 * (*array*) This array combined with the new items.
610 ### Examples:
612         var animals = ['Cow', 'Pig', 'Dog'];
613         animals.combine(['Cat', 'Dog']); //animals = ['Cow', 'Pig', 'Dog', 'Cat'];
617 Array method: erase {#Array:erase}
618 ----------------------------
620 Removes all occurrences of an item from the array.
622 ### Syntax:
624         myArray.erase(item);
626 ### Arguments:
628 1. item - (*object*) The item to search for in the array.
630 ### Returns:
632 * (*array*) This array with all occurrences of the item removed.
634 ### Examples:
636         ['Cow', 'Pig', 'Dog', 'Cat', 'Dog'].erase('Dog') // returns ['Cow', 'Pig', 'Cat']
637         ['Cow', 'Pig', 'Dog'].erase('Cat') // returns ['Cow', 'Pig', 'Dog']
641 Array method: empty {#Array:empty}
642 ----------------------------
644 Empties an array.
646 ### Syntax:
648         myArray.empty();
650 ### Returns:
652 * (*array*) This array, emptied.
654 ### Examples:
656         var myArray = ['old', 'data'];
657         myArray.empty(); //myArray is now []
660 Array method: flatten {#Array:flatten}
661 --------------------------------
663 Flattens a multidimensional array into a single array.
665 ### Syntax:
667         myArray.flatten();
669 ### Returns:
671 * (*array*) A new flat array.
673 ### Examples:
675         var myArray = [1,2,3,[4,5, [6,7]], [[[8]]]];
676         var newArray = myArray.flatten(); //newArray is [1,2,3,4,5,6,7,8]
680 Array method: pick {#Array:pick}
681 --------------------------
682 Returns the first defined value of the array passed in, or null.
684 ### Syntax:
686         var picked = [var1, var2, var3].pick();
688 ### Returns:
690 * (*mixed*) The first variable that is defined.
691 * (*null*) If all variables passed in are `null` or `undefined`, returns `null`.
693 ### Example:
695         function say(infoMessage, errorMessage){
696                 alert([errorMessage, infoMessage, 'There was no message supplied.'].pick());
698                 //or more MooTools 1.2 style using Generics
699                 Array.pick([errorMessage, infoMessage, 'There was no message supplied.']);
701         }
702         say(); // alerts 'There was no message supplied.'
703     say('This is an info message.'); // alerts 'This is an info message.'
704     say('This message will be ignored.', 'This is the error message.'); // alerts 'This is the error message.'
707 ### Notes:
709 This is equivalent to *$pick* from MooTools 1.2.
713 Array method: hexToRgb {#Array:hexToRgb}
714 ----------------------------------
716 Converts an hexadecimal color value to RGB. Input array must be the following hexadecimal color format.
717 \['FF', 'FF', 'FF'\]
719 ### Syntax:
721         myArray.hexToRgb([array]);
723 ### Arguments:
725 1. array - (*boolean*, optional) If true is passed, will output an array (e.g. \[255, 51, 0\]) instead of a string (e.g. "rgb(255, 51, 0)").
727 ### Returns:
729 * (*string*) A string representing the color in RGB.
730 * (*array*) If the array flag is set, an array will be returned instead.
732 ### Examples:
734         ['11', '22', '33'].hexToRgb(); // returns 'rgb(17, 34, 51)'
735         ['11', '22', '33'].hexToRgb(true); // returns [17, 34, 51]
737 ### See Also:
739 - [String:hexToRgb](/Types/String/#hexToRgb)
743 Array method: rgbToHex {#Array:rgbToHex}
744 ----------------------------------
746 Converts an RGB color value to hexadecimal. Input array must be in one of the following RGB color formats.
747 \[255, 255, 255\], or \[255, 255, 255, 1\]
749 ### Syntax:
751         myArray.rgbToHex([array]);
753 ### Arguments:
755 1. array - (*boolean*, optional) If true is passed, will output an array (e.g. \['ff', '33', '00'\]) instead of a string (e.g. '#ff3300').
757 ### Returns:
759 * (*string*) A string representing the color in hexadecimal, or 'transparent' string if the fourth value of rgba in the input array is 0 (rgba).
760 * (*array*) If the array flag is set, an array will be returned instead.
762 ### Examples:
764         [17, 34, 51].rgbToHex(); // returns '#112233'
765         [17, 34, 51].rgbToHex(true); // returns ['11', '22', '33']
766         [17, 34, 51, 0].rgbToHex(); // returns 'transparent'
768 ### See Also:
770 - [String:rgbToHex](/Types/String/#rgbToHex)
774 [Function:bind]: /core/Native/Function/#Function:bind
775 [MDC Array]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array
776 [MDC Array:every]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/every
777 [MDC Array:filter]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter
778 [MDC Array:indexOf]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf
779 [MDC Array:map]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map
780 [MDC Array:some]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/some
781 [MDC Array:forEach]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/forEach
782 [Array:every]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/every
783 [Array:filter]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter
784 [Array:indexOf]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf
785 [Array:map]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map
786 [Array:some]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/some