简体   繁体   中英

Not Finding Empty Array Values in Object

I am trying to understand why when I run the following code, none of the props return as true for being arrays. addresses and emails should return true I would think, and yet they return false .

let obj1 = {
  name: 'John',
  age: 42,
  addresses: [],
  emails: []
}


function findArrays(obj) {
  for (let propName in obj) {
    console.log(propName, Array.isArray(propName));
    // All values console.log as false
 }
}

findArrays(obj1);

You iterate over the keys of the object (which are all strings, eg "emails" ). The arrays are values of the object . To access the value stored under a specific key, use obj[propName] .

propName是字符串,但obj[propName]是数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM