最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论