Skip to main content

Command Palette

Search for a command to run...

Array & Object Destructuring

Updated
1 min read
Array & Object  Destructuring

Destructuring

  1. The main goal of restructuring is to convert it into smaller objects.

How does this work

  1. Place the element that you want to destructure on the right-hand side of ' equals to '
  2. const [ myarray1, myarray2 ] = [1,2,34] Based on the position of the element in the array to the left of the equal to the same number element will be assigned to it

Note: 1. What I mean is myarray1 is the first element of the array.

     2.  Hence the first element of the array on the right side of  equal to will be assigned to it 
  1. Using the rest operator with array destructuring.

const [ myarray1,...rest]=[1,2,3,4]

Now myarray1 will have the value of the first element of the array ie: myarray1 will be equal to 1. and now whatever is left those s elements 2,3,4 will be assigned to rest

Object destructring

  1. As in array destructuring we are doing it based on the elements of the array , so now in object the only difference is we will be doing it on the basis of the object key_value

More from this blog

Web Development

103 posts

Array & Object Destructuring