Destructure array to object property keys

前端 未结 6 1545
渐次进展
渐次进展 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 18:48

    Using destructuring assignment it is possible to assign to an object from an array

    Please try this example:

    const numbers = {};
    
    [numbers.one, numbers.two, numbers.three] = [1, 2, 3]
    
    console.log(numbers)

    The credit to the boys of http://javascript.info/ where I found a similar example. This example is located at http://javascript.info/destructuring-assignment in the Assign to anything at the left-side section

提交回复
热议问题