📢 数组中forin迭代到的属性类型为字符串类型

forof

  1. let a = [4, 5, 6, 7];
  2. for (let v of a) {
  3. console.log(v, typeof v)
  4. }
  5. // 4 number
  6. // 5 number
  7. // 6 number
  8. // 7 number

forin

  1. let a = [4, 5, 6, 7];
  2. for (let idx in a) {
  3. console.log(idx, typeof idx)
  4. }
  5. // 0 string
  6. // 1 string
  7. // 2 string
  8. // 3 string
  9. typeof 'e' // 'string'

Set元素可用for-of遍历得到

Set + for of

  1. let a = new Set();
  2. a.add(1);
  3. a.add(2);
  4. for(let key of a) console.log(key, typeof key)
  5. // 1 number
  6. // 2 number
  7. for(let key in a) console.log(key, typeof key)
  8. // none