Destructure array to object property keys

前端 未结 6 1553
渐次进展
渐次进展 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:02

    I don't believe there's any structuring/destructuring solution to doing that in a single step, no. I wanted something similar in this question. The old := strawman proposal doesn't seem to have legs in the new proposal list, so I don't think there's much activity around this right now.

    This answer shows a very neat two-step version.

    But if it's two steps, you may as well use a simple object initializer:

    const arr = [1,2,3];
    const obj = {
      one: arr[0],
      two: arr[1],
      three: arr[2]
    };
    console.log(obj);

提交回复
热议问题