DE Structuring of Array
Destructuring is a appropriate way of extracting multiple values from data that stored in objects and Arrays.
It can be used in locations that receive data.
It makes possible to unpack values from arrays, or properties from objects, into different variables.
It enables extraction of requested elements from the array and assigning them to variables.
Example of DE Structuring of Array to print first and second values:-
function Test1([first])
{
console.log('First element is ' + first)
}
function Test2([first, , Second])
{
console.log('first element is ' + first + ', Second element is ' + Second)
}
function Test3([,first, , Second])
{
console.log('first element is ' + first + ', Second element is ' + Second)
}
var A = [1, 2, 3, 4, 5]
Test1(A)
Test2(A)
Test3(A)
Output:-
First element is 1
first element is 1, Second element is 3
first element is 2, Second element is 4
Another Example of DE Structuring of Array to print first and second values:-
let [x, , y] = [10,20,30];
console.log(x)
console.log(y)
Output:-
10
30
DE Structuring of Object
It enables extraction of requested properties from the object and assigning them to variables of the same name as properties.
Example of DE Structuring of object to print data:-
function Test({Name, Address, Department})
{
console.log('Name is = ' + Name +' , Department is = ' + Department)
}
let Data =
{
Name: 'Heera',
age: 42,
Address: 'Ambala',
Department: 'Engineer'
}
Test(Data)
Output:-
Name is = Heera , Department is = Engineer