함수 map()

map()

1 JavaScript[ | ]

JavaScript
Copy
var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
console.log( roots );   // roots is now [1, 2, 3]
console.log( numbers ); // numbers is still [1, 4, 9]
[1, 2, 3] 
[1, 4, 9] 
JavaScript
Copy
var numbers = [1, 5, 10, 15];
var doubles = numbers.map(function(x) {
   return x * 2;
});
console.log( doubles ); // doubles is now [2, 10, 20, 30]
console.log( numbers ); // numbers is still [1, 5, 10, 15]
[2, 10, 20, 30] 
[1, 5, 10, 15] 

2 PHP[ | ]

PHP
Copy
$a = [2, 3, 4];
$b = array_map( function($n) { return $n*$n; }, $a);
print_r( $b );
Loading

3 Python[ | ]

Python
Copy
numbers = [1, 2, 3, 4, 5, 6]
print( list(map(lambda x: x * 2 - 1, numbers)) ) # [1, 3, 5, 7, 9, 11]
Loading
Python
Copy
numbers = [1, 2, 3, 4, 5, 6]
print( [x * 2 - 1 for x in numbers] ) # [1, 3, 5, 7, 9, 11]
Loading

4 Perl[ | ]

Perl
Copy
my @numbers = (1, 2, 3, 4, 5, 6);
print map { $_* 2 - 1 } @numbers; # 1357911
Loading

5 R[ | ]

R
Copy
sapply(c(2,3,4), function(n) { n*n }) ## [1]  4  9 16
Loading

6 같이 보기[ | ]