zoukankan      html  css  js  c++  java
  • 对象遍历的几种方法

    1.js对象 用for in遍历 如:for(let item in st){ console.log(item) } // 返回的是键也是就是属性名。

      如果要返回的是键值,则 for(let item in st ) { console.log(st[item]) } // 此时依次输出键值

    let persons = {
      120: { name: 'bob', age: 16, class: 2},
      130: { name: 'lucy', age: 18, class: 1},
      140: { name: 'mary', age: 14, class: 2},
      150: { name: 'jhon', age: 19, class: 1},
      160: { name: 'jack', age: 17, class: 2}
    }
    for(let item in this.persons) { if(this.persons[item].name === 'jack') {   console.log(`Hi, ${this.persons[item].name}, 最近好吗`); // "Hi, jack, 最近好吗" } }

    2.数组对象用for of 遍历时 for(let item of arr){} // 返回为值。

    3.Set 对象用for of 遍历时 for(let item of arr){} // 返回为可以说是键也可以说是值 因为他的键和值 是一样的。

     Set实例对象的values() keys()方法遍历返回的都是一样的,原因是Set实例键名和键值是一样的,,假设arr为Set对象的实例,如下:

     for(let item of arr.keys()) {} // 遍历返回键名

     for(let item of arr.values()) {} // 遍历返回键值

     for(let item of arr.entries()) {} // 返回键值对组成的数组,如:['key', 'value']

    4.Map对象用for of 遍历时 for(let item of arr){} // 返回为键值对的数组。

    引自:https://www.cnblogs.com/woshinidaye/p/6753851.html

  • 相关阅读:
    16.什么是面向对象编程?
    15.运动
    14.this指向和ES6常用内容
    13.正则表达式
    12.事件模型
    11.event事件对象
    10.BOM
    9.DOM
    拓扑排序学习(复习)笔记
    [Luogu] P1987 摇钱树
  • 原文地址:https://www.cnblogs.com/secretAngel/p/9700892.html
Copyright © 2011-2022 走看看