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

  • 相关阅读:
    java笔试之输出
    构造块和静态块[转]
    Java 对象和类
    StringBuffer
    基于bootstrap的表格数据展示
    弹窗式页面
    读取xml
    更新xml
    写XML
    遍历一个类的字段和值
  • 原文地址:https://www.cnblogs.com/secretAngel/p/9700892.html
Copyright © 2011-2022 走看看