gilbertsavier
08-06-2009, 03:27 AM
Hi,
Applies the callback to the elements in given arrays.
function array_map( callback ) {
02. // Applies the callback to the elements in given arrays.
0*. //
04. // version: *05.**22
05. // discuss at: http://phpjs.org/functions/array_map
06. // + original by: Andrea Giammarchi (http://webreflection.blogspot.com)
07. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
08. // + improved by: Brett Zamir (http://brett-zamir.me)
0*. // % note *: Takes a function as an argument, not a function's name
*0. // % note 2: If the callback is a string, it can only work if the function name is in the global context
**. // * example *: array_map( function(a){return (a * a * a)}, [*, 2, *, 4, 5] );
*2. // * returns *: [ *, 8, 27, 64, *25 ]
**. var argc = arguments.length, argv = arguments;
*4. var j = argv[*].length, i = 0, k = *, m = 0;
*5. var tmp = [], tmp_ar = [];
*6.
*7. while (i < j) {
*8. while (k < argc){
**. tmp[m++] = argv[k++][i];
20. }
2*.
22. m = 0;
2*. k = *;
24.
25. if( callback ){
26. if (typeof callback === 'string') {
27. callback = this.window[callback];
28. }
2*. tmp_ar[i++] = callback.apply(null, tmp);
*0. } else{
**. tmp_ar[i++] = tmp;
*2. }
**.
*4. tmp = [];
*5. }
*6.
*7. return tmp_ar;
*8.}
Applies the callback to the elements in given arrays.
function array_map( callback ) {
02. // Applies the callback to the elements in given arrays.
0*. //
04. // version: *05.**22
05. // discuss at: http://phpjs.org/functions/array_map
06. // + original by: Andrea Giammarchi (http://webreflection.blogspot.com)
07. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
08. // + improved by: Brett Zamir (http://brett-zamir.me)
0*. // % note *: Takes a function as an argument, not a function's name
*0. // % note 2: If the callback is a string, it can only work if the function name is in the global context
**. // * example *: array_map( function(a){return (a * a * a)}, [*, 2, *, 4, 5] );
*2. // * returns *: [ *, 8, 27, 64, *25 ]
**. var argc = arguments.length, argv = arguments;
*4. var j = argv[*].length, i = 0, k = *, m = 0;
*5. var tmp = [], tmp_ar = [];
*6.
*7. while (i < j) {
*8. while (k < argc){
**. tmp[m++] = argv[k++][i];
20. }
2*.
22. m = 0;
2*. k = *;
24.
25. if( callback ){
26. if (typeof callback === 'string') {
27. callback = this.window[callback];
28. }
2*. tmp_ar[i++] = callback.apply(null, tmp);
*0. } else{
**. tmp_ar[i++] = tmp;
*2. }
**.
*4. tmp = [];
*5. }
*6.
*7. return tmp_ar;
*8.}