色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

javascript12種循環(huán)

在JavaScript語(yǔ)言中,循環(huán)是實(shí)現(xiàn)重復(fù)執(zhí)行某個(gè)代碼塊的重要工具。常用的循環(huán)語(yǔ)句有for循環(huán)、while循環(huán)、do-while循環(huán)等,但實(shí)際上JavaScript中有12種循環(huán)語(yǔ)句,讓我們一起來(lái)看一下它們吧!

1. for循環(huán):

<code>for (let i = 0; i < 10; i++) {
console.log(i);
}</code>

2. while循環(huán):

<code>let i = 0;
while (i < 10) {
console.log(i);
i++;
}</code>

3. do-while循環(huán):

<code>let i = 0;
do {
console.log(i);
i++;
} while (i < 10);</code>

4. forEach循環(huán):

<code>const arr = ['apple', 'banana', 'orange'];
arr.forEach((item, index) => {
console.log(index, item);
});</code>

5. for…in循環(huán):

<code>const obj = {name: 'Tom', age: 18};
for (let key in obj) {
console.log(key, obj[key]);
}</code>

6. for…of循環(huán):

<code>const arr = ['apple', 'banana', 'orange'];
for (let item of arr) {
console.log(item);
}</code>

7. Generator函數(shù):

<code>function* fibs() {
let a = 0;
let b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
const fib = fibs();
for (let i = 0; i < 10; i++) {
console.log(fib.next().value);
}</code>

8. async/await:

<code>async function foo() {
for (let i = 0; i < 5; i++) {
console.log(i);
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
foo();</code>

9. 循環(huán)標(biāo)簽:

<code>loop: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
console.log(i, j);
if (j === 1) {
break loop;
}
}
}</code>

10. Array.from():

<code>const arr = Array.from({length: 5}, (item, index) => index);
console.log(arr);</code>

11. Array.prototype.reduce():

<code>const arr = [1, 2, 3, 4, 5];
const sum = arr.reduce((prev, curr) => prev + curr, 0);
console.log(sum);</code>

12. Array.prototype.filter():

<code>const arr = [1, 2, 3, 4, 5];
const evenArr = arr.filter(item => item % 2 === 0);
console.log(evenArr);</code>
以上就是JavaScript中的12種循環(huán)語(yǔ)句,它們的使用方式因場(chǎng)景而異,不同的循環(huán)語(yǔ)句可以實(shí)現(xiàn)不同的功能,只有掌握了它們才能更加熟練地進(jìn)行JavaScript編程。