JavaScript使用笔记

1.如何从forEach()方法中跳出循环?
iteratingChildrens = (children: any) => {
let key = ''
if(children && children.length > 0){
try{
children && children.forEach((item: any) => {
if(item.edit){
key = item.nodeId
//异常抛出后会到catch中然后继续向下执行
throw new Error("StopIteration");
}else{
this.iteratingChildrens(item.children)
}
})
}catch(e){
if(e.message == "StopIteration"){
return key
}
}
}
return key;
}

你可能感兴趣的:(JavaScript使用笔记)