javascript 循环数组的六种方式

 循环数组的六种方式 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);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值