zoukankan      html  css  js  c++  java
  • [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; }
    ];
  • 相关阅读:
    第四次博客
    第三次作业
    第二次作业
    入学的第一次作业
    第四次作业
    第三次作业
    第二次随笔作业
    第一次随笔
    第四次作业
    第三次作业
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3887282.html
Copyright © 2011-2022 走看看