How to iterate over a JavaScript object?

后端 未结 18 1690
失恋的感觉
失恋的感觉 2020-11-21 22:52

I have an object in JavaScript:

{
    abc: \'...\',
    bca: \'...\',
    zzz: \'...\',
    xxx: \'...\',
    ccc: \'...\',
    // ...
}

I

18条回答
  •  醉话见心
    2020-11-21 23:09

    If you want the key and value when iterating, you can use a for...of loop with Object.entries.

    const myObj = {a: 1, b: 2}
    
    for (let [key, value] of Object.entries(myObj)) {
        console.log(`key=${key} value=${value}`)
    }
    
    // output: 
    // key=a value=1
    // key=b value=2
    

提交回复
热议问题