关于js的嵌套循环优化

Loui_Zhong 2018-09-03 04:54:37
我用遍历一个树数据,嵌套了很多层循环,但是下一级循环的条件都是一样的,能不能有大神帮忙看看怎么优化
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;
}),
...全文
579 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
天际的海浪 2018-09-03
  • 打赏
  • 举报
回复
类似

function recursion(arr) {
arr.forEach(function(item) {
if (item.children) {
recursion(item.children);
}
});
}
recursion(chapterTree);
天际的海浪 2018-09-03
  • 打赏
  • 举报
回复
用递归的方式遍历
丰云 2018-09-03
  • 打赏
  • 举报
回复
通过属性或样式类名什么的,一次就搞定了。。。。

唉。。。开动脑筋吧。。。。

87,909

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧