Using Objects in For Of Loops

后端 未结 14 2081
春和景丽
春和景丽 2020-11-28 05:56

Why isn\'t is possible to use objects in for of loops? Or is this a browser bug? This code doesn\'t work in Chrome 42, saying undefined is not a function:

te         


        
14条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 06:08

    How about using Object.keys to get an array of keys? And then forEach on the Array?

    obj = { a: 1, b:2}
    Object.keys(obj).forEach( key => console.log(`${key} => ${obj[key]}`))
    

提交回复
热议问题