Demolishing Data Structures: Exploring the Art of Destructuring Arrays and Objects
what is Destructing, why do we need it, and the need for Destructing?
Destructing is used to assign the element of an array or the properties of an object to the individual separate variable.
const tittle=["bhuyan", "Kumar", "dash"]
if we want to assign the value of the first element of an array into a new variable, we can easily do by creating a new variable as,
const abhiTittle=tittle[0]
and for the next one same step, we need to perform, which is quite repetitive and not a Good Approach in terms of the readability of the code, it can be done in a small amount of data but is not efficient in large complex data structures.
So we can use Destructing syntax here
const [abhiTittle,abhishekTittle]=tittle
Why do we need it?
Destructuring allows you to extract values from arrays and objects using a concise syntax, which makes your code more readable and easier to understand.
Destructuring allows you to assign values to variables in a flexible way. You can extract only the values you need, and assign them to variables with meaningful names.
Destructuring allows you to manipulate object properties more easily. You can extract properties from an object and create a new object with only the properties you need.
Destructuring allows you to manipulate arrays more easily. You can extract values from an array and create a new array with only the values you need.
Object Destructing
const { resName } = restuarent;
console.log(resName);//Guess the output
Object-Nested Destructure Example
const {
sat: { morning, eve },
} = gymWeekendOpingHours;
console.log(morning, eve);
//guess the output
Array-Nested Destructing
const [, , , [aa, bb, cc]] = number;
console.log(aa, bb, cc);// Guess the Output
**Some important points need to be in consideration**
Array Destructing - [] / use this Backet
Object Destructing -{} /use this Backet
The object is not iterable, so the sequence to access its properties does not matter