0

I think the reply is surely simple but I am lock on it :/ I want to send a array of array from my server.js to my view.ejs and I need it at the array format. here my code :

in server.js

.get('/map', async function(req, res) {
    let areaCoordinates= await getCoordinates();
    res.render('map.ejs', {token: tokenMapbox, areaCoordinates: areaCoordinates});
})

The array of arrays is areaCoordinates (lets say it's [[[1,2],[3,4]],[[5,6],[7,8]]] but longer and with a length which can change, the length of the array in array also can change only the array in array in array is always 2 (x and y coordinates))

in view.ejs

<script>
    /* some stuff */
    let areaCoord_1="<%= areaCoordinates[1] %>";
    let areaCoord_2= "<%= areaCoordinates[2] %>";
    /* some stuff */
</script>

my problem is for exemple areaCoord_1 = "1,2,3,4" and areaCoord_2="5,6,7,8" when I would need [[1,2],[3,4]] and [[5,6],[7,8]]

I also try in view.ejs

let areaCoord_1=[]
"<%= for (coord of areaCoordinates[1]) { %>"
areaCoord_1.push(coord);
"<%= } %>"

but then i have a syntax error.

does someone have a solution ?
(I think about transforming the string in array and every 2 element making another array for remake the array of array but probably there is cleanier solution and less ressource consuming)

Important point is that I really need an array and not a string in outpout

  • 1
    if its an array encode it. this will work `let areaCoord_1=<%= JSON.stringify(areaCoordinates[1]) %>;` – Lawrence Cherone Oct 25 '19 at 14:46
  • Thank you @LawrenceCherone for a fast reply, unfortunatly It give me a string "[[1,2],[3,4]]" and I need an array [[1,2],[3,4]] such as areaCoord_1[1]=[3,4] (with JSON.stringify it give me areaCoord_1[1]='[' ) –  Oct 25 '19 at 14:51
  • dont wrapt in quotes – Lawrence Cherone Oct 25 '19 at 14:54
  • `[[[1,2],[3,4]],[[5,6],[7,8]]]`? then you need `areaCoordinates[1][0]` for the `[1,2]` set. – Lawrence Cherone Oct 25 '19 at 14:56
  • let areaCoord_1=<%= JSON.stringify(areaCoordinates[1]) %>; without quote return 6 syntax error in visual code (expression expected) efen if it retunr an array –  Oct 25 '19 at 14:57
  • 1
    it will, VS code is not perfect, and the ejs linting lib is worse. this is touching fundamental basic javascript of accessing an array, the rest is just JSON.stringify to place on the page as a string in the form of Javascript Object Notation or you should use an endpoint to grab the data via ajax – Lawrence Cherone Oct 25 '19 at 14:59
  • Ok working so if you want to write it in aswer I will validate your answer ;) I cannot validate in comment –  Oct 25 '19 at 15:15
  • 1
    Possible duplicate of [Passing an object to client in node/express + ejs?](https://stackoverflow.com/questions/11151632/passing-an-object-to-client-in-node-express-ejs) – Lawrence Cherone Oct 25 '19 at 15:26
  • @LawrenceCherone look like yes but without your explanation here it didn't look obvious, so thank you for all your previous comment –  Oct 28 '19 at 16:11

1 Answers1

0

Cf. the comments of Lawrence Cherone under the question :
Using :

let areaCoord_1=<%= JSON.stringify(areaCoordinates[1]) %>; 

without quotes
also look at Passing an object to client in node/express + ejs for similar problem