[removed] Convert Array to Object

后端 未结 6 1311
执念已碎
执念已碎 2021-01-16 03:19

Which is the easiest way to convert this:

[{src:\"websrv1\"}, {dst:\"websrv2\"}, {dstport:\"80\"}]

to this:

{src:\"websrv1\         


        
6条回答
  •  自闭症患者
    2021-01-16 03:51

    Don't use this! but just for fun

    var a = [{src:"websrv1"}, {dst:"websrv2"}, {dstport:"80"}];
    var f = a.reduce((c,d) => Object.assign(c,d), {})
    

    The tiny drawback is that a is mutated with an infinite recursive object but, who cares? it works in one line!

提交回复
热议问题