简体   繁体   中英

Jade render an array of strings as an array of string

I'm passing data to render template:

res.render 'index', {data: ['a', 'b']}, function(err, html) {
});

in the template, I'd like to render the array ['a', 'b'] as an array in javascript:

script(type='text/javascript').
  var arr = #{data};

but they are rendered as [a, b] , an array of variables, what I want is variable names: ['a', 'b'] .

Thanks

First you need to JSON.stringify the data property object.

...
data: JSON.stringify(['a', 'b'])
...

Then in your Jade Template use !{} .

var arr = !{data}; // ["a","b"];

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