簡體   English   中英

在 JavaScript 中循環返回函數的語句

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

我試圖使用谷歌地圖構建一個熱圖,我寫了這段代碼,但現在你可以看到我正在將硬編碼值傳遞給它,我想在 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)
    ];
}

我們可以在 JavaScript 中的 return 語句中使用循環語句嗎? 如果是,那么如何?

不,這是不可能的。 但是為什么不先構建數組然后返回它呢?

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

你不能在 JavaScript 中,但你只需在服務器上使用循環來打印 JavaScript return 語句中的值。

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

或者你可以這樣做:

<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>

函數內部的 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;
}

如果您的數據不是硬編碼的,您可以執行一個循環,在將數據傳遞給返回之前,將數據添加到 arr (數組)。 例子:

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

上面代碼的返回結果將是0-10的數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM