【发布时间】:2013-08-13 16:15:09
【问题描述】:
var updateIconPathRecorsive = function (item) {
if (item.iconSrc) {
item.iconSrcFullpath = 'some value..';
}
_.each(item.items, updateIconPathRecorsive);
};
updateIconPathRecorsive(json);
有没有更好的不使用函数的方法? 我不想将函数从调用中移开,因为它就像一个复杂的 for。我可能希望能够在以下行中写一些东西:
_.recursive(json, {children: 'items'}, function (item) {
if (item.iconSrc) {
item.iconSrcFullpath = 'some value..';
}
});
【问题讨论】:
-
您需要在某个时候引用该函数。你的第一个代码 sn-p 对我来说很好。
-
所以基本上你想要的是递归所有对象属性的迭代器?
标签: javascript underscore.js lodash