简体   繁体   English

需要将数组的元素添加到JSP中的JavaScript变量中

[英]Need to add elements of an array to a variable of JavaScript in JSP

I am creating an array for a list of markers of google map. 我正在为Google地图标记列表创建数组。 The code works for one forEach loop but I need to add elements of a new array to the list as well. 该代码适用于一个forEach循环,但是我还需要向列表中添加新数组的元素。

My current code shows following error message on var of result2. 我当前的代码在result2的var上显示以下错误消息。

  Syntax error on token "var", ;  expected

Code for one array 一个数组的代码

var results = [
        <c:forEach var="cus" items="${customer}" varStatus="loop">[
            "${cus.value.name}", "${cus.value.latitude}",
            "${cus.value.longitude}", "${loop.index}",
            "${cus.value.id}"], </c:forEach> ];

Code for two arrays 两个数组的代码

var results = "[" +
                <c:forEach var="cus" items="${customer}" varStatus="loop">[
                        "${cus.value.name}", "${cus.value.latitude}",
                        "${cus.value.longitude}", "${loop.index}",
                        "${cus.value.id}"], </c:forEach> 

var results2 = results + <c:forEach var="staff" items="${staff}" varStatus="loop"> 
        + "[" +
                  "${staff.value.name}", "${staff.value.latitude}",
                  "${staff.value.longitude}", "${loop.index}",
                  "${staff.value.id}"], </c:forEach> 
        +"]";

First of all, in your code for two arrays you don't want to have square brackets enclosed in the quotation marks. 首先,在两个数组的代码中,您不想在引号中包含方括号。 This will make your whole arrays result and result2 to be a Strings. 这将使您的整个数组resultresult2为字符串。
Second, closing bracket is missing for results , opening bracket is missing for results2 . 第二, results缺少results2括号, results2缺少results2括号。
Third, you cannot just + the arrays in JavaScript. 第三,不能只用JavaScript +数组。 There's a concat() method for that. 有一个concat()方法。

Try this: 尝试这个:

var results = [
    <c:forEach var="cus" items="${customer}" varStatus="loop">
    [
        "${cus.value.name}",
        "${cus.value.latitude}",
        "${cus.value.longitude}",
        "${loop.index}",
        "${cus.value.id}"
    ],</c:forEach>
];

var results2 = results.concat([
    <c:forEach var="staff" items="${staff}" varStatus="loop">
    [
        "${staff.value.name}",
        "${staff.value.latitude}",
        "${staff.value.longitude}",
        "${loop.index}",
        "${staff.value.id}"
    ],</c:forEach>
]);

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

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