node.js ejs如何迭代对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31764552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ejs how to iterate object
提问by Boaz Hoch
I have a simple object literal which is address as shown here
我有一个简单的对象文字,它的地址如下所示
address: {
country: String,
state: String,
city: String,
zip: String,
street: String
}
and its inside an object which i'm passing with express.js render function.
并且它在我使用 express.js 渲染函数传递的对象中。
in my template page i'm trying to for loop inside this object as shown:
在我的模板页面中,我试图在这个对象内进行循环,如图所示:
<% for (var prop in artist.address ) { %>
<%- artist.address[prop] %>
<% } %>
which output the data but includes the ejs functions as well like so:
它输出数据,但也包括 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 ===
so how do i need to iterate my object?
那么我需要如何迭代我的对象?
采纳答案by Doron Segal
using plain JS you can use Object.keys
使用普通的 JS 你可以使用Object.keys
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']
In your example
在你的例子中
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'
});
Another cool way is to use lodash: lodash forEach
另一个很酷的方法是使用 lodash:lodash forEach
_([1, 2]).forEach(function(n) {
console.log(n);
}).value();
回答by mscdex
You're seeing all of the inherited properties in addition to the "own" properties you've added on top.
除了您在顶部添加的“自己的”属性之外,您还会看到所有继承的属性。
There's two ways to solve this. One is to use hasOwnProperty()to ensure you don't see inherited properties:
有两种方法可以解决这个问题。一种是用来hasOwnProperty()确保您看不到继承的属性:
<% for (var prop in artist.address) {
if (Object.prototype.hasOwnProperty.call(artist.address, prop)) { %>
<%- artist.address[prop] %>
<% }
} %>
Or use Object.keys()which returns an array of only non-inherited properties and iterate over that:
或者使用Object.keys()which 返回一个仅包含非继承属性的数组并对其进行迭代:
<% Object.keys(artist.address).forEach(function(prop) { %>
<%- artist.address[prop] %>
<% }); %>
Since this is mongoose related, you may also try iterating over artist.address.toObject()(using the public API) or artist.address._doc(using a private API) or perhaps up a level on the artistobject.
由于这是与猫鼬相关的,您也可以尝试迭代artist.address.toObject()(使用公共 API)或artist.address._doc(使用私有 API)或在artist对象上向上一层。
回答by Boaz Hoch
OK so i dived into it here is an explanation, i had an object:
好的,所以我潜入这里是一个解释,我有一个对象:
address : {
country: String,
state: String,
city: String,
zip: String,
street: String
}
i needed to display only those properties and not inherited once so i iterate over the object and got its own properties:
我只需要显示那些属性而不是继承一次,所以我遍历对象并获得自己的属性:
<% Object.keys(artist.address).forEach(function(prop) { %>
// ["country" , "state" , "city" etc ]
<%- artist.address[prop] %> // so artist.address.state logs "New York City"
<% }); %>
but the problem was that my artist.addressobject had two more properties:
each one holds a function with a return.
但问题是我的artist.address对象还有两个属性:每个属性都包含一个带返回值的函数。
function () { return this.get(path); } function () { return this.get(path); }
so i checked for properties that holds a string like so:
所以我检查了包含这样一个字符串的属性:
<% Object.keys(artist.address).forEach(function(prop) {
if( typeof artist.address[prop] == "string" ) { %>
<%- artist.address[prop] %>
<% } %>
<% }); %>

