PHP前端开发

JavaScript For 循环示例

百变鹏仔 2个月前 (10-14) #JavaScript
文章标签 示例

标准 for 循环

for (let i = 0; i <h2>      for...of 循环(遍历数组)</h2><pre class="brush:php;toolbar:false">const fruits = ['apple', 'banana', 'orange'];for (const fruit of fruits) {  console.log(fruit);

for...in 循环(迭代对象属性)

const person = {  name: 'john',  age: 30,  job: 'developer'};for (const key in person) {  console.log(`${key}: ${person[key]}`);}

foreach循环(数组方法)

const numbers = [1, 2, 3, 4, 5];numbers.foreach((number, index) =&gt; {  console.log(`index ${index}: ${number}`);});

while 循环(严格来说不是 for 循环,但值得一提)

let count = 0;while (count <p>这些示例演示了在 javascript 中进行迭代的各种方法,每种方法都适合不同的场景和数据结构。</p>