How to iterate over a JavaScript object?

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

I have an object in JavaScript:

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

I

18条回答
  •  北海茫月
    2020-11-21 23:28

    Yes. You can loop through an object using for loop. Here is an example

    var myObj = {
        abc: 'ABC',
        bca: 'BCA',
        zzz: 'ZZZ',
        xxx: 'XXX',
        ccc: 'CCC',
    }
    
    var k = Object.keys (myObj);
    for (var i = 0; i < k.length; i++) {
        console.log (k[i] + ": " + myObj[k[i]]);
    }

    NOTE: the example mentioned above will only work in IE9+. See Objec.keys browser support here.

提交回复
热议问题