网站备案信息更改,做网站要学什么语言,惠州百度seo哪家好,牛商网官网文章目录 创建数组获取数组的长度遍历数组使用普通for循环来遍历数组使用ES5 for in循环使用ES6 for of循环使用forEach来进行迭代箭头函数测试遍历数组 创建数组
括号内的数字是数组的长度
let ar new Array(4)
//向数组添加元素
ar[0] 100
ar[1] true
ar[2] etoak
ar[3… 文章目录 创建数组获取数组的长度遍历数组使用普通for循环来遍历数组使用ES5 for in循环使用ES6 for of循环使用forEach来进行迭代箭头函数测试遍历数组 创建数组
括号内的数字是数组的长度
let ar new Array(4)
//向数组添加元素
ar[0] 100
ar[1] true
ar[2] etoak
ar[3] null获取数组的长度
//let ar new Array(4)
console.log(arr.length)//打印数组长度遍历数组
使用普通for循环来遍历数组
for(let i 0;iar.length;i){console.log(数组元素值是${ar[i]})
}使用ES5 for in循环
如果是单值集合那么这个key就是索引值 如果是键值对那么这个key就是属性名
for(let key in ar){console.log(数组索引值是${key})console.log(数组元素值是${ar[key]})
}使用ES6 for of循环
for(let value of 循环体){//这个value就是值
}注意此循环不能用来迭代我们自己创建的js对象 因为我们自己的创建的对象底层没有迭代器
使用forEach来进行迭代
/*循环体.forEach(function(每个被遍历的对象,索引值){//业务逻辑})
*/
ar.forEach(function(value,index){console.log(索引值是${index})console.log(数组元素值是${value})
})箭头函数
在ES6中匿名函数可以省略为箭头函数ar.forEach(valueXXXX)
匿名函数去掉function在参数后添加
如果只有一个参数则括号括号省略 如果大括号内直接是return 语句则大括号省略 return 省略
测试遍历数组
!DOCTYPE html
html langzh-CNheadmeta charsetUTF-8title数组/titlestylediv{width:200px;height:100px;background-color: coral;}/style/headbody!--onmouseover:鼠标滑过--div onmouseovershowArray()测试域/divscriptfunction showArray(){let ar new Array(4)ar[0] 100ar[1] truear[2] etoakar[3] null//普通forfor(let i 0;iar.length;i){console.log(数组元素值是${ar[i]})}// for infor(let key in ar){console.log(数组索引值是${key})console.log(数组元素值是${ar[key]})}//for offor(let value of ar){console.log(数组元素值是${value})}//forEachar.forEach(function(value,index){console.log(索引值是${index})console.log(数组元素值是${value})})//forEach 箭头函数版ar.forEach(valueXXXX)}/script/body
/html