foreach跳出循环
forEach跳出循环可以用for in(遍历key)或者for of(遍历value)替代
let success = false; // 成功与否
for (const key in msgList) {
let newMsg = msgList[key];
try {
// 相关code
success = true;
newAttr.push(newMsg);
} catch (error) {
remote.dialog.showErrorBox('提示', '操作失败');
success = false;
return false;
}
}
摘自: javascript——forEach跳出循环
理解for…of vs. for…in 语句
for…of和for…in均可迭代一个列表;但是用于迭代的值却不同,for…in迭代的是对象的 键 的列表,而for…of则迭代对象的键对应的值。
下面的例子展示了两者之间的区别:
let list = [4, 5, 6];
for (let i in list) {
console.log(i); // "0", "1", "2",
}
for (let i of list) {
console.log(i); // "4", "5", "6"
}
还没有评论,来说两句吧...