JS basics - how to traverse an object

        The for in syntax is generally used to traverse objects. The key in the for in syntax is a variable, which represents the attribute name of the object in turn during the loop process. Since the key is a variable, it must be parsed using the [ ] syntax.

        key is the attribute name of the obtained object,

        The object name [key] is to obtain the attribute value of the object.

Code example:

    // 1.声明一个对象
    let object = {
      uname: '张三',
      age: 18,
      gender: '男'
    }
    // 2.使用for in 遍历对象
    for (let key in object) {
      console.log(key)  // 结果是属性名:uname age gender 
      console.log(object[key]) // 结果是属性值:张三 18 男
    }

Guess you like

Origin blog.csdn.net/2202_75324165/article/details/130172293