关于js的嵌套循环优化
我用遍历一个树数据,嵌套了很多层循环,但是下一级循环的条件都是一样的,能不能有大神帮忙看看怎么优化
chapterTreeData: Ember.computed("model", function() {
var chapterTree = this.get("model.titleTreeModel");
var that = this;
chapterTree.forEach(function(item) {
if (item.children) {
item.children.forEach(function(item) {
that.chapterTreeDataEach(item);
if (item.children) {
item.children.forEach(function(item) {
that.chapterTreeDataEach(item);
if (item.children) {
item.children.forEach(function(item) {
that.chapterTreeDataEach(item);
if (item.children) {
item.children.forEach(function(item) {
that.chapterTreeDataEach(item);
if (item.children) {
item.children.forEach(function(item) {
that.chapterTreeDataEach(item);
});
}
});
}
});
}
});
}
});
}
});
return chapterTree;
}),