问题 ejs如何迭代对象


我有一个简单的对象文字,其地址如下所示

address: {
    country: String,
    state: String,
    city: String,
    zip: String,
    street: String
}

它的内部是一个我用express.js渲染函数传递的对象。

在我的模板页面中,我正在尝试在此对象内循环,如下所示:

<% for (var prop in artist.address ) { %>
  <%- artist.address[prop] %>
<% } %>

它输出数据但包括ejs函数,如下所示:

function () { return this.get(path); } function () { return this.get(path); } yafo 09988 jerusalem israel israeli [object Object] undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined [object Object] [object Object] function () { var self = this , hookArgs // arguments eventually passed to the hook - are mutable , lastArg = arguments[arguments.length-1] , pres = this._pres[name] , posts = this._posts[name] , _total = pres.length , _current = -1 , _asyncsLeft = proto[name].numAsyncPres , _next = function () { if (arguments[0] instanceof Error) { return handleError(arguments[0]); } var _args = Array.prototype.slice.call(arguments) , currPre , preArgs; if (_args.length && !(arguments[0] == null && typeof lastArg === 

那我怎么需要迭代我的对象?


1586
2017-08-01 17:54


起源

这里的答案很好: stackoverflow.com/questions/14379274/javascript-iterate-object - snozza
是 address 实际上是 mongoose.Schema 和 artist.address 是一个 mongoose.Document? - mscdex
不,艺术家是mongoose.schema var artistSchema = mongoose.Schema({address:{country:String,state:String,city:String,zip:String,street:String},.... - Boaz Hoch


答案:


使用普通JS你可以使用 Object.keys

var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']

在你的例子中

var artist = { address: { city: 'Tel Aviv' } };
Object.keys(artist.address).forEach(function(key){
  <%- artist.address[city] %> //key will city the output will be 'Tev Aviv' 
});

另一个很酷的方法是使用lodash: lodash forEach

    _([1, 2]).forEach(function(n) {
  console.log(n);
}).value();

6
2017-08-03 05:37



这回答了如何在Javascript中迭代,OP标题问题是如何使用EJS进行迭代。对于那些在标题中寻找问题答案的人来说,这个答案是误导性的。 - Cleanshooter


除了您在顶部添加的“自有”属性之外,您还可以看到所有继承的属性。

有两种方法可以解决这个问题。一个是使用 hasOwnProperty() 确保您没有看到继承的属性:

<% for (var prop in artist.address) {
     if (Object.prototype.hasOwnProperty.call(artist.address, prop)) { %>
       <%- artist.address[prop] %>
<%   }
   } %>

或者使用 Object.keys() 它返回一个只有非继承属性的数组并迭代它:

<% Object.keys(artist.address).forEach(function(prop) { %>
  <%- artist.address[prop] %>
<% }); %>

由于这是与猫鼬相关的,你也可以尝试迭代 artist.address.toObject() (使用公共API)或 artist.address._doc (使用私有API)或者可能上升到一个级别 artist 目的。


8
2017-08-01 18:05



仍在做:<%Object.keys(artist.address).forEach(function(prop){%> <% - artist.address [prop]%> <%}); %>返回:function(){return this.get(path); } function(){return this.get(path); yafo 09988 - Boaz Hoch
意外物业的名称是什么? - mscdex


好的,所以我潜入它这里是一个解释, 我有一个对象:

address : {
  country: String,
  state: String,
  city: String,
  zip: String,
  street: String
}

我需要只显示那些属性而不是继承一次所以我遍历对象并获得自己的属性:

<% Object.keys(artist.address).forEach(function(prop) { %>
   // ["country" , "state" , "city" etc ]
  <%- artist.address[prop] %> // so artist.address.state logs "New York City"
<% }); %>

但问题是我的 artist.address object有两个属性: 每个人都拥有一个带回归的功能。

function () { return this.get(path); } function () { return this.get(path); }

所以我检查了包含如此字符串的属性:

<% Object.keys(artist.address).forEach(function(prop) {
  if( typeof artist.address[prop] == "string" ) { %>
    <%- artist.address[prop] %>
  <% } %>
<% }); %> 

2
2017-08-03 17:59



你需要弄清楚你想什么时候使用ejs客户端或服务器。如果你在服务器端使用它并且让它说明:返回对象应该如下所示:controller:return res.render('template / something.ejs',{_:require('lodash'),artist:artistDocument });查看模板<%_。(artist.address).forEach(function(prop){%> <% - artist.address [prop]%> <%}); %>客户端只使用cdn服务获取lodash <script type =“// cdn / lodash ...”> - Doron Segal