Categories
JavaScript Answers

How to destructure an object and ignore one of the results with JavaScript?

Spread the love

Sometimes, we want to destructure an object and ignore one of the results with JavaScript.

In this article, we’ll look at how to destructure an object and ignore one of the results with JavaScript.

How to destructure an object and ignore one of the results with JavaScript?

To destructure an object and ignore one of the results with JavaScript, we can use the object rest operator.

For instance, we write:

const props = {
  a: 1,
  b: 2,
  c: 3
};
const {
  a,
  ...propsNoA
} = props;
console.log(propsNoA);

We have the props object with a few properties in it.

Then we destructure props by destructuring a and leave the rest of the properties in one object with the rest operator, which is the 3 dots.

propsNoA is an object with properties b and c inside.

Therefore, propsNoA is {b: 2, c: 3} according to the console log.

Conclusion

To destructure an object and ignore one of the results with JavaScript, we can use the object rest operator.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *