0

I have an array in a PostGreSQL query and I can't turn it into a session variable.

The table resulting from the query sends this: {1,2,3}

if I transform this into a session variable I have the same thing.

If in my query, I transform my array into a string, my session variable is: ['1','2','3']

When I use this session variable in javascript code and want to generate strings from it, it returns this: (sess_modules is my session variable)

for (i=0; i<sess_modules.length; i++) {
    var module = `<div class="leaflet-sidebar-pane" id="module_${i+1}"></div>`
    liste_modules.push(module);

    $("#mydiv").html("${liste_modules}")
}

That's works but I have commas (cf picture). Why ?

I would like to know why my code returns commas too.

HTML code:

enter image description here

5
  • Assuming you are using a string template, not the literal string "${liste_modules}" -> `${liste_modules}` - when you coerce from an array to a string without specifying how-to, js will output that array with commas. See this with: var x = [1,2,3]; console.log("" + x) Commented Sep 22, 2023 at 7:29
  • @freedomn-m : How can I change this ? Thks Commented Sep 22, 2023 at 7:32
  • Your issue is that you are outputting the array for every iteration. Change $("#mydiv").html("${liste_modules}") to $("#mydiv").append(module) and remove list_moduiles Commented Sep 22, 2023 at 7:34
  • Or move the .html(..) line outside the loop and use $("#mydiv").html(liste_modules.join("")) to join without a comma Commented Sep 22, 2023 at 7:35
  • 1
    That works. I used the first code. Thank you for your help. have a nice day. Sylvain Commented Sep 22, 2023 at 7:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.