简体   繁体   中英

spring mvc multiple array values append inslide [[]] using javascript

I'm working spring mvc with mongoDb

I got multiple image name values from the collection:

Ex: 1.jpg, 2.jpg, 3.jpg....

Question:

Now I want this values to inside the [[]]

Ex : [["1.jpg","2.jpg","3.jpg"]]

My code

<script type="text/javascript"> 
 var slices = [];
 <c:forEach var="items" items="${LiteCOLL.dwi}" varStatus="status">
    <c:forEach items="${items.TTP}" var="item">
        slices.push("${item}");     
    </c:forEach>
 </c:forEach>  

    var params = [];
    var data = [["1.jpg","2.jpg","3.jpg"]];
    params["images"] = data;

</script>

I found some solutions but I can't able to resolve it

In your code you already get an array, what you need is an array of array, so what you need to do is just create a new array and push the "slices" in it.

<script type="text/javascript"> 
 var slices = [];
 <c:forEach var="items" items="${LiteCOLL.dwi}" varStatus="status">
    <c:forEach items="${items.TTP}" var="item">
        slices.push("${item}");     
    </c:forEach>
 </c:forEach>  

  var result = [slices];
</script>
function loadFun(){
    var dbData = '1.jpg, 2.jpg, 3.jpg';
    var splitStr = dbData.split(',');

    var firstArray = [];
    for(var i=0; i<splitStr.length; i++) {
        firstArray.push(splitStr[i]);
    }


    var secondArray = [];
    secondArray.push(firstArray);

    console.log(JSON.stringify(secondArray));
}

1st need to split then You need to declear two array and need to push one by one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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