I am building an app using node and the jade template engine. For some of my pages I'd like to display some JSON items passed by my server:

            res.render('index', {
                items: myJSONitems
            });

My problem is that I don't know the keys of my item beforehand. How can I display all the fields of my JSONs?

I have thought of using something like this but I don't know how to use it in Jade:

        var itemKeys = [];
        if (items.length > 0) itemKeys = Object.keys(items[0].data);

        res.render('index', {
            items: items, item_keys: itemKeys
        });
有帮助吗?

解决方案

Iterate over an object while retrieving both keys and values in Jade using the following syntax:

each value, key in obj
  h4=key
  p=value

See the documentation on iteration in Jade as well.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top