How to pass an object from pug to front end javascript
First stringify your object using JSON.stringify
:
res.render('view/edit', { title: 'Title', sub: true, data: JSON.stringify(variableObject) });
Then use String Interpolation, Unescaped !{data}
script(type='text/javascript'). var x = !{data} console.log(x);
Or just do it all once, in your template:
script(type='text/javascript'). var x = !{JSON.stringify(data)} console.log(x);
(kudos to @Matt, Thanks)
In this case I used this:
var x = "#{ JSON.stringify(y) }" console.log( JSON.parse(x.replace(/"/g,'"')) );
I´m not sure if this is the best practice.