zl程序教程

您现在的位置是:首页 >  Javascript

当前栏目

前端测试题:关于for、for-in、for-of说法不正确的是?

2023-02-26 09:47:35 时间

考核内容: 遍历

题发散度:

试题难度:

解题:

遍历

什么叫数组的遍历:就是把数组中的元素依次取出来过程

方法1: 基础for循环

  1. for(let i = 0; i < 数组名称.length; i++) {
  2. console.log(数组名称[i]);
  3. // i从0开始,到最大索引,所以可以拿到数组的每个元素。
  4. }

方法2: 遍历数组的元素 for of

  1. for(let ele of 数组){ //ele 数组中的元素
  2. console.log(ele);
  3. }

方法3: 枚举出数组的索引 for in 也可以用于 对象的循环

  1. for(let index in 数组){ //index 是数组的下标
  2. console.log(index,数组[index]);
  3. }
  4. for(key in {name:"shuke"}){

    console.log(key)

  5. }

D项for 循环 不可以利用下标遍历对象,但是for in 循环可以

在线测试:

答案:

D:for 循环 可以利用下标遍历对象