简体   繁体   English

玉在迭代中获取对象键名

[英]jade get object keyname in iteration

var data = {
  thing: 'value1',
  thing2: 'value2',
  thing3: 'value3'
};

each val, index in data
    li= val.keyname??

The output I want is: 我想要的输出是:

<li>thing</li>
<li>thing2</li>
<li>thing3</li>

Is this possible? 这可能吗?

I tried just using 'index', but it iterated on the entire list of properties and prototype functions attached to the object, eg $__, isNew, errors, _doc, $__original_save, save, _pres, _posts, toObject, toJSON, $__isNested etc.. 我尝试仅使用“索引”,但是在附加到对象的属性和原型函数的整个列表上进行了迭代,例如$__, isNew, errors, _doc, $__original_save, save, _pres, _posts, toObject, toJSON, $__isNested等等..

The data is from part of a find query from MongoDB. data来自MongoDB的find查询的一部分。

In my usecase I do not know the names of the keys beforehand. 在我的用例中,我事先不知道键的名称。

Data must be a simple key-value object. 数据必须是简单的键值对象。

If You have most complicated object (like model or collection) use a toJSON pattern like in Backbone.Model or Backbone.Collection . 如果你有最复杂的物体(如模型或集合)使用toJSON图案像Backbone.ModelBackbone.Collection

If You have a simple object, use each val, idx like this: 如果您有一个简单的对象,请使用each val, idx如下所示:

ul
  each val, idx in data
    li= idx

Full code: http://jsfiddle.net/tomi77/5rb5hspk/2/ 完整代码: http//jsfiddle.net/tomi77/5rb5hspk/2/

Use library UnderscoreJS's invert method. 使用UnderscoreJS库的invert方法。 Underscorejs.org Underscorejs.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM