for循環(huán)
- 使用break,可以中斷循環(huán)
let arr = [1,2,3,4,5,6,7,8,9]
for(let i = 0; i< arr.length; i++){
if(i === 5){
break;
}
console.log(arr[i])
}
- 使用continue硅瞧,循環(huán)可以繼續(xù)
let arr = [1,2,3,4,5,6,7,8,9]
for(let i = 0; i< arr.length; i++){
if(i === 5){
continue;
}
console.log(arr[i])
}
for循環(huán)使用continue.PNG
- 使用return榛斯,會報(bào)錯
let arr = [1,2,3,4,5,6,7,8,9]
for(let i = 0; i< arr.length; i++){
if(i === 5){
return;
}
console.log(arr[i])
}
for...in 循環(huán)
- 使用break,可以跳出循環(huán)
let arr = [1,2,3,4,5,6,7,8,9]
for(let i in arr){
if(i == 5){
break;
}
console.log(arr[i])
}
for..in循環(huán)使用break.PNG
- 使用continue
let arr = [1,2,3,4,5,6,7,8,9]
for(let i in arr){
if(i == 5){
continue;
}
console.log(arr[i])
}
for...in使用continue繼續(xù)循環(huán).PNG
- 使用return,報(bào)錯
let arr = [1,2,3,4,5,6,7,8,9]
for(let i in arr){
if(i == 5){
return;
}
console.log(arr[i])
}
for...in使用return報(bào)錯.PNG
for...of循環(huán)
- 使用break畸裳,
let arr = [1,2,3,4,5,6,7,8,9]
for(let item of arr){
if(item == 5){
break;
}
console.log(item)
}
- 使用continue,會跳出當(dāng)前循環(huán)繼續(xù)循環(huán)
let arr = [1,2,3,4,5,6,7,8,9]
for(let item of arr){
if(item === 5){
continue;
}
console.log(item)
}
- return ,報(bào)錯
let arr = [1,2,3,4,5,6,7,8,9]
for(let item of arr){
if(item == 5){
return;
}
console.log(item)
}
forEach
- 使用break,報(bào)錯
let arr = [1,2,3,4,5,6,7,8,9]
arr.forEach((item) => {
if(5 === item){
break;
}
console.log(item)
})
- 使用continue,報(bào)錯
let arr = [1,2,3,4,5,6,7,8,9]
arr.forEach((item) => {
if(5 === item){
continue;
}
console.log(item)
})
- 使用return會跳出當(dāng)前循環(huán)给涕,但不會終止循環(huán)
let arr = [1,2,3,4,5,6,7,8,9]
arr.forEach((item) => {
if(5 === item){
return;
}
console.log(item)
})
filter
- 使用break,報(bào)錯
let arr = [1,2,3,4,5,6,7,8,9]
let result = []
result = arr.filter((item) => {
if(5 === item){
break;
}
return item
})
console.log(result)
- 使用continue,報(bào)錯
let arr = [1,2,3,4,5,6,7,8,9]
let result = []
result = arr.filter((item) => {
if(5 === item){
continue;
}
return item
})
console.log(result)
- 使用break够庙,會跳出當(dāng)前循環(huán)恭应,繼續(xù)
let arr = [1,2,3,4,5,6,7,8,9]
let result = []
result = arr.filter((item) => {
if(5 === item){
return;
}
return item
})
console.log(result)
map
- 使用break,報(bào)錯
let arr = [1,2,3,4,5,6,7,8,9]
let result = []
result = arr.map((item) => {
if(5 === item){
break;
}
return item
})
console.log(result)
- 使用continue耘眨,報(bào)錯
let arr = [1,2,3,4,5,6,7,8,9]
let result = []
result = arr.map((item) => {
if(5 === item){
continue;
}
return item
})
console.log(result)
- 使用return昼榛,不會報(bào)錯
let arr = [1,2,3,4,5,6,7,8,9]
let result = []
result = arr.map((item) => {
if(5 === item){
return;
}
return item
})
console.log(result)