How to change data array to be object using javasc

2019-09-20 13:00发布

I have data like this {"black":["/file/d60f198e-dde5-4e5e-b2ab-1ee10c96a027.png"]}

how to change data to be "black" : "/file/d60f198e-dde5-4e5e-b2ab-1ee10c96a027.png" ? I'm using javascript. Thanks advance

2条回答
forever°为你锁心
2楼-- · 2019-09-20 13:02

A generic solution would be something like:

var data = {
   black: ['abc'],
   red: ['xyz']
}

var result = _.mapObject(data, _.first)

which would give you:

{
    black: 'abc',
    red: 'xyz'
}
查看更多
太酷不给撩
3楼-- · 2019-09-20 13:04

Just set that property to be the value of the array:

var original = {"black":["/file/d60f198e-dde5-4e5e-b2ab-1ee10c96a027.png"]}
original.black = original.black[0]
查看更多
登录 后发表回答