# 循环总结

### 条件循环

##### - while循环 使用break结束循环
``````let num = 1;
while (num < 10) {
console.log(num)
if (num > 5) {
break
}
num++
}
``````
##### - do while循环 不管条件真假至少执行一次使用break结束循环
``````let number = 0
do {
console.log(number)
if (number > 6) {
break
}
number++
} while (number < 10)
``````

### 数组遍历

##### - for 用于遍历数组 break跳出整个循环，continue结束当前循环
``````for (let i = 0; i < 10; i++) {
if (i === 5) {
continue
}
console.log(i)
}
``````
##### - for of //可以遍历Array Map Set String TypedArray,字符串，不能遍历对象
``````const arr = [
{name: 'zs', age: 25},
{name: 'ls', age: 26}
]
let string = 'abcdef'
for (const objElement of string) {
console.log(objElement)
}
``````
##### - forEach //无返回值
``````const eachArr = [1, 2, 3, 4, 8, 9]
eachArr.forEach((item, index, arr) => {
console.log(`\${index}-\${item}`)
})
``````
##### - map 返回一个新数组
``````const newArr = eachArr.map((item, index, arr) => {
return item * 2
})
console.log(newArr)
``````

### 对象遍历

##### - for in 遍历对象,特点是不仅能读取自身的key，而且能读取继承原型链上的key,可以通过obj.hasOwnProperty(keys)来验证是不是自身的属性
``````const obj = {
name: 'zs',
age: 25,
sex: 'girl'
}
const objarr = [1, 2, 3]
for (const objKey in objarr) {
console.log(objarr[objKey ])
}
``````
##### - 下面的对象遍历返回的返回的都是数组
``````Object.keys(obj)    //返回的是对象的键
Object.values(obj)  //返回的是对象的值
Object.entries(obj) //返回一个二维数组 键值
``````