javascript - How do I iterate over a JSON array using Jade and Node.js - Stack Overflow

So I have this JSON array apiData being passed on to the view as data.Backendrouter.get('&#

So I have this JSON array apiData being passed on to the view as data.

Backend

router.get('/', function(req, res) {
    var data = JSON.stringify(apiData);
    res.render('gallery', { data: apiData });
}); 

Frontend

extends layout

block content
    h1 MyProject
    !{JSON.stringify(data)}

I am trying to cache !{JSON.stringify(data)} in a variable and iterate through it in the jade file. I am pletely new to jade. How could I go about doing this?

So I have this JSON array apiData being passed on to the view as data.

Backend

router.get('/', function(req, res) {
    var data = JSON.stringify(apiData);
    res.render('gallery', { data: apiData });
}); 

Frontend

extends layout

block content
    h1 MyProject
    !{JSON.stringify(data)}

I am trying to cache !{JSON.stringify(data)} in a variable and iterate through it in the jade file. I am pletely new to jade. How could I go about doing this?

Share Improve this question asked Jan 26, 2015 at 11:51 ng-hacker-319ng-hacker-319 6992 gold badges6 silver badges8 bronze badges 2
  • You could use Objects.key(data) and for (k in Objects.key(data)){//access to each value like; data[k];} – kahonmlg Commented Jan 26, 2015 at 11:56
  • Could you post a link to code at repl.it or jsfiddle? – ng-hacker-319 Commented Jan 26, 2015 at 12:02
Add a ment  | 

1 Answer 1

Reset to default 5

You have a few problems in your code, like not using the stringification you do server side at all.

But you don't even need JSON here. Simply pass the array and iterate on it :

Backend

router.get('/', function(req, res) {
    res.render('gallery', { data: apiData });
}); 

Frontend

extends layout

block content
    h1 MyProject
        each thing in data
             p= thing

You'll find examples of iteration in the documentation

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742277082a4413791.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信