Seven ES6 deconstruction code techniques to make your code more concise

If you are tired of writing bloated and hard-to-read code, and want to improve your coding skills and make your code more readable and concise, then today's article is for you. In today's content In , I'll take you deep into the world of ES6 deconstruction and share with you how to use it to write cleaner, more efficient code.

If you are tired of writing bloated and hard-to-read code, and want to improve your coding skills and make your code more readable and concise, then today's article is for you. In today's content In , I'll take you deep into the world of ES6 deconstruction and share with you how to use it to write cleaner, more efficient code.

Let's get started now.

1. Destructuring objects

One of the most common ways to use destructuring objects is to assign the object's properties to variables. For example:

const person = { name: 'John', age: 30 };
const name = person.name;
const age = person.age;

And if we want the code to be more concise, we can write it like this using destructuring:

const person = { name: 'John', age: 30 };
const { name, age } = person;

2. Destructuring the array

Just like objects, we can also use destructuring to assign elements of arrays to variables. For example:

const numbers = [1, 2, 3];
const first = numbers[0];
const second = numbers[1];
const third = numbers[2];

And if we want the code to be more concise, we can write it like this using destructuring:

const numbers = [1, 2, 3];
const [first, second, third] = numbers;

3. Default value

Destructuring can also assign a default value to a variable in cases where the value is not defined, for example:

const person = { name: 'John' };
let age = person.age || 25;

If we use destructuring to write the code more concisely:

const person = { name: 'John' };
const { age = 25 } = person;

4. Rename variables

Sometimes, the property or variable name we're trying to destructure doesn't match the name we're using in our code. In these cases, you can use a colon (:) to rename the variable. For example:

const person = { firstName: 'John', lastName: 'Doe' };
const first = person.firstName;
const last = person.lastName;

We can use destructuring to make the code more concise, as follows:

const person = { firstName: 'John', lastName: 'Doe' };
const { firstName: first, lastName: last } = person;

5. Nested deconstruction

Destructuring also works on nested objects and arrays. For example,

const data = {
    results: [
        {
            title: 'Article 1',
            author: {
                name: 'John',
                age: 30
            }
        },
        {
            title: 'Article 2',
            author: {
                name: 'Jane',
                age: 25
            }
        }
    ]
};
const firstResultTitle = data.results[0].title;
const firstAuthorName = data.results[0].author.name;
const firstAuthorAge = data.results[0].author.age;

And we use nested destructuring to make the code more concise, as follows:

const data = {
  results: [
    {
      title: 'Article 1',
      author: {
        name: 'John',
        age: 30
      }
    },
    {
      title: 'Article 2',
      author: {
        name: 'Jane',
        age: 25
      }
    }
  ]
};


const {
results: [{ title: firstResultTitle, author: { name: firstAuthorName, age: firstAuthorAge } }]
} = data;

6. Destructuring function parameters

Destructuring can also be used on function parameters, for example,

function createPerson(options) {
const name = options.name;
const age = options.age;
// ...
}


createPerson({ name: 'John', age: 30 });

And we use the destructor function parameter, written like this:

function createPerson({ name, age }) {
// ...
}


createPerson({ name: 'John', age: 30 });

7. Destructuring and Spreading Operators

We can also use the spread operator (…) in conjunction with destructuring to assign the remaining elements of an array or the remaining properties of an object to a variable, for example,

const numbers = [1, 2, 3, 4, 5];
const [first, second, ...others] = numbers;
console.log(others); // [3, 4, 5]

We can also use the spread operator and destructuring to make the code more concise, as follows:

const numbers = [1, 2, 3, 4, 5];
const [first, second, ...others] = numbers;
console.log(others); // [3, 4, 5]

Summarize

ES6 destructuring is a powerful tool that can help us write cleaner and more readable code. Combining these 7 tips shared in this article, I think you will be able to deconstruct objects and arrays, use default values, rename variables, and even combine deconstruction with the spread operator to help us improve work efficiency.

In conclusion, our key to writing clean and concise code is to always strive for simplicity and readability, so next time you're writing JavaScript, try these destructuring techniques and see how they can improve your code's cleanliness.

Guess you like

Origin blog.csdn.net/weixin_42232156/article/details/129934988