How to create new array with the results of calling a function on every element from other array in JavaScript

2 Answers

0 votes
var numbers = [1, 4, 9, 81];
var arr = numbers.map(Math.sqrt);

console.log(arr); 


/*
run:  
 
 [1, 2, 3, 9]
  
*/

 



answered May 21, 2016 by avibootz
0 votes
var numbers = [1, 4, 9, 81];
var arr = numbers.map(function(n) {
  return n * 2;
});

console.log(arr); 


/*
run:  
 
 [2, 8, 18, 162]
  
*/

 



answered May 21, 2016 by avibootz
...