循环数组的六种方式 for in, for of, for, forEach, map, filter
其中for in, for of, for 能直接使用break, continue中断循环
forEach, map, filter 不能使用break continue中断循环,而是使用 return flase,try ctach 抛出异常 来中断循环
tip:函数作为参数的循环,直接拿数组的值!!
1.for in 可以使用break continue
let list = [
{name:'cookie'},
{age: 18}
]
// for in 可以使用break continue
for(let i in list) {
console.log(i); // 0 1
console.log(list[i]); // {name:'cookie'}, {age: 18}
if(list[i].name == 'cookie') {
break
}
console.log(list[i]); //没有值输出 break 满足条件,直接退出循环
if(list[i].name == 'cookie') {
continue
}
console.log(list[i]); // {age: 18}
}
2.for of 能使用break continue
// for of 能使用break continue
for (const i of list) {
console.log(i); // {name:'cookie'}, {age: 18}
if(i.name == 'cookie') {
break
}
console.log(i); //没有值输出 break 满足条件,直接退出循环
if(i.name == 'cookie') {
continue // 中断此次循环 但是不会跳出循环
}
console.log(i); // {age: 18}
}
3.for 可以使用break continue
// for 可以使用break continue
for (let i = 0; i < list.length; i++) {
console.log(i); // 0 1
console.log(list[i]) // {name:'cookie'}, {age: 18}
if(list[i].name == 'cookie') {
break
}
console.log(list[i]); //没有值输出 break 满足条件,直接退出循环
if(list[i].name == 'cookie') {
continue
}
console.log(list[i]); // {age: 18}
}
4. forEach不能使用break continue, 我们使用return 中止此次循环,try ctach 抛出异常 来结束整个循环
// forEach不能使用break continue, 我们使用return 中止此次循环,try ctach 抛出异常 来结束整个循环
list.forEach((i,index) => {
console.log(i); // {name:'cookie'}, {age: 18}
console.log(index); // 0 1
if(list[i].name == 'cookie') {
break // forEach不能使用break Uncaught SyntaxError: Illegal break statement
}
console.log(list[i]);
if(list[i].name == 'cookie') {
continue // forEach不能使用continue Uncaught SyntaxError: Illegal continue statement: no surrounding iteration statement
}
console.log(list[i]);
if(i.name == 'cookie') {
return false // 中断此次循环 但是不会跳出循环
}
console.log(i); // {age: 18}
});
try{
let list = [
{name:'cookie'},
{age: 18}
]
list.forEach(i => {
if(i.name == 'cookie') {
throw Error('跳出循环')
}
console.log(i);
});
}catch(e) {
console.log(e);
}
5.filter 不能使用break continue, 我们使用return 中止此次循环,try ctach 抛出异常 来结束整个循环
// filter 不能使用break continue, 我们使用return 中止此次循环,try ctach 抛出异常 来结束整个循环
list.filter((i,index)=>{
console.log(i); // {name:'cookie'}, {age: 18}
console.log(index);// 0 1
if(i.name == 'cookie') {
// break // forEach不能使用break Uncaught SyntaxError: Illegal break statement
continue // forEach不能使用continue Uncaught SyntaxError: Illegal continue statement: no surrounding iteration statement
}
console.log(i);
})
try{
let list = [
{name:'cookie'},
{age: 18}
]
list.filter(i => {
if(i.name == 'cookie') {
throw Error('跳出循环')
}
console.log(i);
});
}catch(e) {
console.log(e);
}
6.map 不能使用break continue, 我们使用return 中止此次循环,try ctach 抛出异常 来结束整个循环
// map 不能使用break continue, 我们使用return 中止此次循环,try ctach 抛出异常 来结束整个循环
list.map((i,index)=>{
console.log(i); // {name:'cookie'}, {age: 18}
console.log(index); // 0 1
if(i.name == 'cookie') {
// break // forEach不能使用break Uncaught SyntaxError: Illegal break statement
continue // forEach不能使用continue Uncaught SyntaxError: Illegal continue statement: no surrounding iteration statement
}
console.log(i); //没有值输出 break 满足条件,直接退出循环
if(i.name == 'cookie') {
return false // 中断此次循环 但是不会跳出循环
}
console.log(i); // {age: 18}
})
try{
let list = [
{name:'cookie'},
{age: 18}
]
list.map(i => {
if(i.name == 'cookie') {
throw Error('跳出循环')
}
console.log(i);
});
}catch(e) {
console.log(e);
}