简体   繁体   English

在 JavaScript 中循环返回函数的语句

[英]Loop in return statement of a function in JavaScript

I was trying to built a heat map using google map and I write this code but now as you can see I am passing hard coded values to it, I want to use a loop in return statement because I am using MySQL and all the info will be stored in the database so when I call this function it should exactly create as much points on the map as many values are stored in the database.我试图使用谷歌地图构建一个热图,我写了这段代码,但现在你可以看到我正在将硬编码值传递给它,我想在 return 语句中使用循环,因为我使用的是 MySQL 并且所有信息都将存储在数据库中,所以当我调用这个函数时,它应该在地图上创建与数据库中存储的值一样多的点。

function getPoints() 
{
    return [
        new google.maps.LatLng(28.81820630, 77.05907876),
        new google.maps.LatLng(28.8097835, 77.05673218),
        new google.maps.LatLng(28.8097835, 77.003688),
        new google.maps.LatLng(28.8097835, 77.002815),
        new google.maps.LatLng(28.782992, 77.002112),
        new google.maps.LatLng(28.783100, 77.001061),
        new google.maps.LatLng(28.783206, 77.000829),
        new google.maps.LatLng(28.783273, 77.000320),
        new google.maps.LatLng(28.783316, 77.000023),
        new google.maps.LatLng(28.783357, 77.039790),
        new google.maps.LatLng(28.783281, 77.039687),
        new google.maps.LatLng(28.783368, 77.039666),
        new google.maps.LatLng(28.783383, 77.039590),
        new google.maps.LatLng(28.783508, 77.039525),
        new google.maps.LatLng(28.783802, 77.039591),
        new google.maps.LatLng(28.780107, 77.039668),
        new google.maps.LatLng(28.780206, 77.039686),
        new google.maps.LatLng(28.780386, 77.039790),
        new google.maps.LatLng(28.780701, 77.039902),
        new google.maps.LatLng(28.780965, 77.039938),
        new google.maps.LatLng(28.785010, 77.039907),
        new google.maps.LatLng(28.785360, 77.039952),
        new google.maps.LatLng(28.785715, 77.000030),
        new google.maps.LatLng(28.786117, 77.000119),
        new google.maps.LatLng(28.786560, 77.000209),
        new google.maps.LatLng(28.786905, 77.000270),
        new google.maps.LatLng(29.786905, 77.000270)
    ];
}

Can we use loop statements in JavaScript in return statement ?我们可以在 JavaScript 中的 return 语句中使用循环语句吗? If so then how?如果是,那么如何?

No, it is not possible.不,这是不可能的。 But why you don't first build the array and afterwards return it?但是为什么不先构建数组然后返回它呢?

function getPoints() {
    var array = [];
    for (...) {
        array.push(new google.maps.LatLng(value1, value2));
    }
    return array;
}

You can't in JavaScript but you just use a loop on the server to to print the values inside of your JavaScript return statement.你不能在 JavaScript 中,但你只需在服务器上使用循环来打印 JavaScript return 语句中的值。

<script>
<%! 
        String getPointsFromData() {
            return myGetPointsLoopMethod();
        }
%>
function getPoints() 
{
    return [
       <% getPointsFromData(); %>
    ];
}
</script>

Or you can do this:或者你可以这样做:

<script>

    function getPoints()

        return [
            <%
              InitialContext ctx;
              DataSource ds;
              Connection conn;
              Statement stmt;
              ResultSet rs;

              try {
                ctx = new InitialContext();
                ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MySQLDataSource");
                //ds = (DataSource) ctx.lookup("jdbc/MySQLDataSource");
                conn = ds.getConnection();
                stmt = conn.createStatement();
                rs = stmt.executeQuery("SELECT lat,long * FROM table");

                while(rs.next()) {
            %>
         new google.maps.LatLng(<%= rs.getString("Lat") %>, <%= rs.getString("Lon") %>),
            <%  
                }
              }
              catch (Exception e) {              
              }

            %>
         ]; 
        }
</script>
</body>

The return inside the function can only be called once at a time.函数内部的 return 一次只能调用一次。

function YourFuncName()
{
   var arr =[
    new google.maps.LatLng(28.81820630, 77.05907876),
    new google.maps.LatLng(28.8097835, 77.05673218),
    new google.maps.LatLng(28.8097835, 77.003688),
    new google.maps.LatLng(28.8097835, 77.002815),
    new google.maps.LatLng(28.782992, 77.002112),
    new google.maps.LatLng(28.783100, 77.001061),
    new google.maps.LatLng(28.783206, 77.000829),
    new google.maps.LatLng(28.783273, 77.000320),
    new google.maps.LatLng(28.783316, 77.000023),
    new google.maps.LatLng(28.783357, 77.039790),
    new google.maps.LatLng(28.783281, 77.039687),
    new google.maps.LatLng(28.783368, 77.039666),
    new google.maps.LatLng(28.783383, 77.039590),
    new google.maps.LatLng(28.783508, 77.039525),
    new google.maps.LatLng(28.783802, 77.039591),
    new google.maps.LatLng(28.780107, 77.039668),
    new google.maps.LatLng(28.780206, 77.039686),
    new google.maps.LatLng(28.780386, 77.039790),
    new google.maps.LatLng(28.780701, 77.039902),
    new google.maps.LatLng(28.780965, 77.039938),
    new google.maps.LatLng(28.785010, 77.039907),
    new google.maps.LatLng(28.785360, 77.039952),
    new google.maps.LatLng(28.785715, 77.000030),
    new google.maps.LatLng(28.786117, 77.000119),
    new google.maps.LatLng(28.786560, 77.000209),
    new google.maps.LatLng(28.786905, 77.000270),
    new google.maps.LatLng(29.786905, 77.000270)];
   return arr;
}

if your data isn't hard coded you can perform a loop which will add data to arr (array) instead, before passing it to return.如果您的数据不是硬编码的,您可以执行一个循环,在将数据传递给返回之前,将数据添加到 arr (数组)。 example:例子:

function YourFunctionName()
{
     var arr = [];
     for(var i=0; i<10; i++)
     {
         arr.push(i);
     }
     return arr;
}

the return result of the above code will be arrays from 0-10上面代码的返回结果将是0-10的数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM