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

时间:2021-06-08 01:07:28

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; }
];