Destructure array to object property keys

前端 未结 6 1554
渐次进展
渐次进展 2020-12-15 18:40

I have an array of values like:

const arr = [1,2,3];

Is there any way I can use destructuring to create the following output? If not, what

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-15 19:11

    This answers a slightly different requirement, but I came here looking for an answer to that need and perhaps this will help others in a similar situation.

    Given an array of strings : a = ['one', 'two', 'three'] What is a nice un-nested non-loop way of getting this resulting dictionary: b = { one : 'one', two: 'two', three: 'three' } ?

    const b = a.map(a=>({ [a]: a })).reduce((p, n)=>({ ...p, ...n }),{})

提交回复
热议问题