How to iterate over a JavaScript object?

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

I have an object in JavaScript:

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

I

18条回答
  •  甜味超标
    2020-11-21 23:13

    I finally came up with a handy utility function with a unified interface to iterate Objects, Strings, Arrays, TypedArrays, Maps, Sets, (any Iterables).

    const iterate = require('@a-z/iterate-it');
    const obj = { a: 1, b: 2, c: 3 };
    
    iterate(obj, (value, key) => console.log(key, value)); 
    // a 1
    // b 2
    // c 3
    

    https://github.com/alrik/iterate-javascript

提交回复
热议问题