I am trying to take an object from the server-side and bring it to an ejs file to use it in javascript.
The object inside the javascript looks like this:
var span = document.createElement("SPAN");
span.innerHTML = "<%= JSON.stringify(positions) %>";
var positions = span.innerText;
span.remove()
console.log(positions)
and it returns a string like this one:
"
{
'0': { foo: foo },
'1': { foo: boo },
'2': { foo: doo }
}
"
Is there a way for me to do that?
Thanks!
You can just assign a variable to the stringified positions (no quotes) and it becomes a javascript object. Then to display it stringify that object again.
(This "renders it as JS from the outset").
const myJSObj = <%= JSON.stringify(positions) %>;
console.log(JSON.stringify(myJSObj));