[Javascript] Using map() function instead of for loop

As an example, if Jason was riding the roller coaster (and when isn’t he), your goal would be to change his cell from ["Jason", "Millhouse"] to just "Jason Millhouse". Notice that an array goes in to the function you will build, but a string comes out.

var passengers = [ ["Thomas", "Meeks"],
                   ["Gregg", "Pollack"],
                   ["Christine", "Wong"],
                   ["Dan", "McGaw"] ];
var modifiedNames = passengers.map(function(cell){
    return cell[0] +" "+cell[1];
});

 

var modifiedNames = [ "Thomas Meeks", 
                      "Gregg Pollack", 
                      "Christine Wong", 
                      "Dan McGaw" ];

modifiedNames.map(function(cell){
    alert("Yo, "+cell)
});

 

var puzzlers = [
  function ( a ) { return 3*a - 8; }, 
  function ( a ) { return (a+2) * (a+2) * (a+2); }, 
  function ( a ) { return a * a - 9; },
  function ( a ) { return a % 4; }
];

 

posted @ 2014-08-02 18:00  Zhentiw  阅读(208)  评论(0编辑  收藏  举报