详解JS中常用的循环和遍历

1,循环

循环,就是根据某个条件,重复执行一段代码

1.1,for循环

语法:

for (1 声明循环变量; 2 判断循环条件; 3 更新循环变量) {
     // 4 要执行的代码块
}
// 执行顺序 1 -> 2 -> 4 -> 3

例子:

for(let i = 0; i < 10; i++) {
  console.log(i)
  // 输出 0 ~ 9
}
// 或者
for(let i = 0; i < 10 && i != 5; i++) {
  console.log(i)
  // 输出 0 ~ 4
}
// 或者
for(let i = 0; i < 0 || i < 6; i++) {
  console.log(i)
  // 输出 0 ~ 5
}

注意:

  • for循环有三个表达式:1:声明循环变量;2:判断循环条件;3:更新循环变量。三个表达式之间,用;符号分割,表达式都可以省略,但是两个;符号缺一 不可

  • for循环是先判断再执行,与while循环相同

  • for循环的三个表达式都可以有多部分组成,多个判断条件用&&||连接

for循环嵌套

例子:

// 99乘法表
for (let i = 1;i < 10; i++) {
  let arr = []
  for (let k = 1;k < 10; k++) {
    arr.push(`${i}x${k}=${i * k}`)
  }
  console.log(arr)
}

1.2,while循环

语法:

while (条件) {
    要执行的代码块
}

例子:

let num = 0
while (num < 10){
  console.log(num)
  num++
}
// 输出 0 ~ 9

注意:

  • while循环会一直循环代码块,只要指定的条件为true,所以不要忘记对条件中使用的变量进行递增

1.3,do while循环

语法:

do {
    要执行的代码块
}

while (条件);

例子:

let i = 6

do {
  console.log(i)
  i++
}
while (i < 5)

注意:

  • do while循环是while循环的变体。它会先执行一次代码块。之后再判断条件,只要条件为真就会重复循环

  • 该循环至少会执行一次,即使条件为false

1.4,跳出循环

有时候需要跳过一次循环或者是终止整个循环

continue

跳过循环中的一个迭代

例子:

for (i = 0; i < 5; i++) {
  if (i === 3) { continue }
  console.log(i)
}
// 输出 0 1 2 4

注意:

  • for循环中,continue之后执行的语句,是循环变量更新语句i++

  • whiledo while循环中,continue之后执行的语句,是循环条件判断,必须将continue放到i++之后使用,否则continue将跳过i++进入死循环

break

跳出并终止整个循环

例子:

for (i = 0; i < 5; i++) {
  if (i === 2) { break }
  console.log(i)
}

注意:

  • 如果循环有多层,则break只能跳出一层

二,遍历

遍历,是指沿着某条搜索路线,依次对数据中每个节点均做一次访问

2.1,for in

for...in语句用于对数组或者对象的可枚举属性进行循环操作,可用break或者throw跳出

语法:

// '变量'用来指定变量,指定的变量可以是数组元素,也可以是对象的属性

for (变量 in 对象)
{
    在此执行代码
}

例子:

let obj = {
  name: '张三',
  age: 18
}

let arr = ['a', 'b', 'c']

for (let i in obj) {
  console.log(i)
}
// 输出 name age

for (let i in arr) {
  console.log(i)
}
// 输出 0 1 2

2.2,for of

for...of语句在可迭代对象上创建一个迭代循环,调用自定义迭代钩子,并为每个不同属性的值执行语句(包括ArrayMapSetStringTypedArrayarguments对象等等,不包括Object),可用break或者throw跳出

语法:

for (variable of 可迭代对象) {
    // 操作语句
}

例子:

let arr = ['a', 'b', 'c']

let obj = {
  name: '张三',
  age: 18,
  sex: '男'
}

for (let i of arr) {
  console.log(i)
}
// 输出 a b c

for (let i of obj) {
  console.log(i)
}
// 报错 obj is not iterable (obj不是可迭代的)

2.3,forEach

按升序为数组中含有效值的每一项执行一次给定的函数,那些已删除或者未初始化的项将被跳过(比如[1,,2]),如果需要中断,请使用try/catch配合throw

语法:

array.forEach(callback(currentValue, index, array))

例子:

let arr = ['a', 'b', 'c']

arr.forEach((item, index) => {
  console.log(`值:${item} 下标${index}`)
})
// 输出 值:a下标:0  值:b下标:1  值:c下标:2

终止循环:

try {
  arr.forEach((item, index) => {
    if (index === 1) {
      throw '终止'
    }
    console.log(`值:${item}下标:${index}`)
  })
} catch (error) {
  console.log(error)
}
// 输出 值:a下标:0 终止

2.4,map

数组遍历,返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值

语法:

array.map(function(currentValue,index,arr), thisValue)

例子:

let arr = [0, 1, 2, 3, 4, 5]
let val = arr.map((item => {
  return item + 10
}))
console.log(val)
// 输出 [10, 11, 12, 13, 14, 15]

2.5,filter

数组遍历,返回一个新数组,其包含通过所提供函数实现的测试的所有元素

语法:

let newArray = arr.filter(callback(element, index, array), thisArg)

例子:

let arr = [0, 1, 2, 3, 4, 5]
function fn(e) {
  return e - 2 > 1
}
let arr2 = arr.filter(fn)
console.log(arr2)
// 输出 [3, 4, 5]

2.6,keys()

对象遍历,返回一个数组,其元素包括对象自身的(不含继承的)所有可枚举属性(不含Symbol属性)的键名,不会返回原型上的方法

语法:

Object.keys(obj)

例子:

let obj = {
  name: '张三',
  age: 18,
  sex: '男'
}
console.log(Object.keys(obj))
// 输出 ["name", "age", "sex"]

2.7,values()

对象遍历,返回一个由目标对象value组成的数组,其元素是在目标对象上找到的可枚举属性值

语法:

Object.values(obj)

例子:

let obj = {
  name: '张三',
  age: 18,
  sex: '男'
}
console.log(Object.keys(obj))
// 输出 ["张三", 18, "男"]

如果看了觉得有帮助的,我是@鹏多多11997110103,欢迎 点赞 关注 评论;
END



作者:鹏多多11997110103
链接:https://www.jianshu.com/p/004f7521b812
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @ 2021-09-28 10:16  front-gl  阅读(841)  评论(0编辑  收藏  举报