zim*_*t28 7 javascript underscore.js
有没有办法_.omit在嵌套对象属性上使用?
我希望这发生:
schema = {
firstName: {
type: String
},
secret: {
type: String,
optional: true,
private: true
}
};
schema = _.nestedOmit(schema, 'private');
console.log(schema);
// Should Log
// {
// firstName: {
// type: String
// },
// secret: {
// type: String,
// optional: true
// }
// }
Run Code Online (Sandbox Code Playgroud)
_.nestedOmit显然不存在,只是_.omit不影响嵌套属性,但应该清楚我在寻找什么.
它也不必是下划线,但根据我的经验,它通常只会使事情变得更短更清晰.
您可以创建一个nestedOmitmixin来遍历该对象以删除不需要的密钥.就像是
_.mixin({
nestedOmit: function(obj, iteratee, context) {
// basic _.omit on the current object
var r = _.omit(obj, iteratee, context);
//transform the children objects
_.each(r, function(val, key) {
if (typeof(val) === "object")
r[key] = _.nestedOmit(val, iteratee, context);
});
return r;
}
});
Run Code Online (Sandbox Code Playgroud)
和演示http://jsfiddle.net/nikoshr/fez3eyw8/1/
| 归档时间: |
|
| 查看次数: |
2278 次 |
| 最近记录: |