Joined: Fri Jun 26, 2009 5:50 am Posts: 23 Has thanked: 0 time Have thanks: 1 time
Applies the callback to the elements in given arrays. function array_map( callback ) { 02. // Applies the callback to the elements in given arrays. 03. // 04. // version: 905.3122 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) 09. // % note 1: Takes a function as an argument, not a function's name 10. // % note 2: If the callback is a string, it can only work if the function name is in the global context 11. // * example 1: array_map( function(a){return (a * a * a)}, [1, 2, 3, 4, 5] ); 12. // * returns 1: [ 1, 8, 27, 64, 125 ] 13. var argc = arguments.length, argv = arguments; 14. var j = argv[1].length, i = 0, k = 1, m = 0; 15. var tmp = [], tmp_ar = []; 16. 17. while (i < j) { 18. while (k < argc){ 19. tmp[m++] = argv[k++][i]; 20. } 21. 22. m = 0; 23. k = 1; 24. 25. if( callback ){ 26. if (typeof callback === 'string') { 27. callback = this.window[callback]; 28. } 29. tmp_ar[i++] = callback.apply(null, tmp); 30. } else{ 31. tmp_ar[i++] = tmp; 32. } 33. 34. tmp = []; 35. } 36. 37. return tmp_ar; 38.}