Created
August 29, 2023 07:23
-
-
Save taroyanaka/7a8441caa82b17f623e8bbaec3a72d7b to your computer and use it in GitHub Desktop.
continueとbreakはforの中でだけ使える.foreachでは使えない(試してないけどmapとかfilterでも同様)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// you can't do this!!!!!!!!!!! | |
// const array = [1, 2, 3, 4, 5]; | |
// outerLoop: | |
// array.forEach((item) => { | |
// innerLoop: | |
// for (let i = 0; i < 3; i++) { | |
// if (item === 3 && i === 1) { | |
// // 条件に一致する場合はスキップする | |
// continue outerLoop; | |
// } | |
// console.log(item, i); | |
// } | |
// }); | |
// outerLoop: | |
// array.forEach((item) => { | |
// innerLoop: | |
// for (let i = 0; i < 3; i++) { | |
// if (item === 3 && i === 1) { | |
// 条件に一致する場合はスキップする | |
// break outerLoop; | |
// } | |
// console.log(item, i); | |
// } | |
// }); | |
// you can't do this!!!!!!!!!!! | |
// you can do this!!!!!!!!!!! | |
const array = [1, 2, 3, 4, 5]; | |
outerLoop: | |
for (let j = 0; j < array.length; j++) { | |
const item = array[j]; | |
innerLoop: | |
for (let i = 0; i < 3; i++) { | |
if (item === 3 && i === 1) { | |
// 条件に一致する場合はスキップする | |
continue outerLoop; | |
} | |
console.log(item, i); | |
} | |
} | |
// breakを使うサンプル | |
const array2 = [1, 2, 3, 4, 5]; | |
outerLoop: | |
for (let j = 0; j < array2.length; j++) { | |
const item = array2[j]; | |
innerLoop: | |
for (let i = 0; i < 3; i++) { | |
if (item === 3 && i === 1) { | |
// 条件に一致する場合はスキップする | |
break outerLoop; | |
} | |
console.log(item, i); | |
} | |
} | |
// you can do this!!!!!!!!!!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment