Javascript 如何在Sails.js中检测对象是水线模型还是模型集合
我需要遍历对象树。有些对象是水线模型或模型集合,我需要以特定的方式处理它们。那么,如何可靠地检测JavaScript对象是否是Sails.js中的水线模型或集合呢?下面是一个片段。在我的model toJSON()调用中,我需要知道传入的是模型还是json对象。所以我只需要检查associations属性Javascript 如何在Sails.js中检测对象是水线模型还是模型集合,javascript,node.js,sails.js,waterline,Javascript,Node.js,Sails.js,Waterline,我需要遍历对象树。有些对象是水线模型或模型集合,我需要以特定的方式处理它们。那么,如何可靠地检测JavaScript对象是否是Sails.js中的水线模型或集合呢?下面是一个片段。在我的model toJSON()调用中,我需要知道传入的是模型还是json对象。所以我只需要检查associations属性 toJSON: function(users) { var obj = this.toObject(); var arr = _.map(users,
toJSON: function(users) {
var obj = this.toObject();
var arr = _.map(users, function(user) {
if (user.hasOwnProperty('associations')) {
return user.toJSON();
}
else {
return user;
}
});
obj.users = arr;
return obj;
}
这里有一个片段。在我的model toJSON()调用中,我需要知道传入的是模型还是json对象。所以我只需要检查associations属性
toJSON: function(users) {
var obj = this.toObject();
var arr = _.map(users, function(user) {
if (user.hasOwnProperty('associations')) {
return user.toJSON();
}
else {
return user;
}
});
obj.users = arr;
return obj;
}