javascript的三种循环写法:
- 普通遍历
<script>
let arr=[
{name:'小汉',age:18,sex:'男'},
{name:'大壮',age:16,sex:'男'},
{name:'小三',age:20,sex:'男'},
{name:'强强',age:22,sex:'男'}
]
for(let i=0;i<arr.length;i++){
console.log(arr[i]);
}
</script>
- in
for的in返回的i就是数组里每个对象的下标
<script>
let arr=[
{name:'小汉',age:18,sex:'男'},
{name:'大壮',age:16,sex:'男'},
{name:'小三',age:20,sex:'男'},
{name:'强强',age:22,sex:'男'}
]
for(let i in arr){
console.log(arr[i]);
}
</script>
- of
for的of返回的i就是数组的单个对象
<script>
let arr=[
{name:'小汉',age:18,sex:'男'},
{name:'大壮',age:16,sex:'男'},
{name:'小三',age:20,sex:'男'},
{name:'强强',age:22,sex:'男'}
]
for(let i of arr){
console.log(i);
}
</script>