In Javascript a dictionary comprehension, or an Object `map`

前端 未结 6 1421
青春惊慌失措
青春惊慌失措 2020-12-28 13:01

I need to generate a couple of objects from lists in Javascript. In Python, I\'d write this:

{key_maker(x): val_maker(x) for x in a_list}

A

6条回答
  •  醉话见心
    2020-12-28 13:30

    Here's a version that doesn't use reduce:

    Object.fromEntries( a_list.map( x => [key_maker(x), value_maker(x)]) );
    

    Object.fromEntries is basically the same as _.fromPairs in Lodash. This feels the most like the Python dict comprehension to me.

提交回复
热议问题