Destructure array to object property keys

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

    With destructuring, you can either create new variables or assign to existing variables/properties. You can't declare and reassign in the same statement, however.

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

提交回复
热议问题